I am using CoreData. I'm adding date and some datas. I need a if statement. This is if statement will work like that :
"if this date is available in CoreData database, user won't add any data."
I used this:
if newuser.valueForKey(NSDate) as NSDate == NSDate()
This is absolutely wrong. I'm new and i don't create this if statement. how can i do this ?
Thanks already !
To compare the dates you should be using isEqualToDate:
if newuser.valueForKey(NSDate).isEqualToDate(NSDate())
But, dates are very accurate, so the current date would need to match the saved date, and that's never going to happen - at least a part of a second will have passed before you make the comparison.
So, what you really need to do is to find out what day, month and year the date is an compare those.
In a number of ways it would be best to store these values explicitly in Core Data instead of using a date object, though both would work. In either case you need to get the date components from the date in order to find out the day, month and year and then you need to compare them (possibly creating a date with only day, month and year and no time so you can compare it to the stored date which should also have no time set).
Related
I have a problem where i want to set a base date to a date time variable in the database. I would then use the base date for my aggregations. And the days subsequent to this base date would be categorised as Day1, Day2 and so on.
So for example, if I want to see cohorts of orders created by the base date - I would want the calculated after this date. I don't want to do this using a date filter.
Image below:
It seems like you could use the DATE function to convert your field, like this:
DATE([Acquisition Date])
If you don't already have the year value stored, but have a method in mind to add it, you could use MONTH([Acquisition Date]) to obtain the month and date and DAY([Acquisition Date]) to obtain the date. You could then use MAKEDATE function to build the date or MAKEDATETIME function to build the datetime value.
Once it's stored as a date value, you could use DATEADD to add and subtract days to it as needed.
There are multiple formulas available for converting Tableau dates, described here: https://help.tableau.com/current/pro/desktop/en-us/functions_functions_date.htm
Is this what you're looking for?
I am new to this iOS world, trying to learn how to handle dates and time.
Imagine I have a Class Shop. The shop have time-intervals which represent the open and close time for each day of the week.
Some context data (example string from database, GMT Timezone):
Monday: "08:00:00-13:00:00, 15:00:00-18:00:00"
Tuesday:"09:00:00-13:00:00, 15:00:00-19:00:00"
Wednesday: "15:00:00-23:59:59"
Thursday: "00:00:00-08:00:00"
etc..
Monday for example would have to store 2 time-intervals.
My question is how can I store this data (array of DateIntervals? TimeIntervals? or another more suitable class?) in a Class and get the current time to check if the store is opened or not.
The native date format for iOS (and Mac OS) is the Date object. A Date object represents and instant in time, independent of time zone. You then use a DateFormatter to convert a date to a string representation in a particular time zone.
In your case, though, you need to represent timer ranges for days of the week on a variety of different dates.
You should read the Calendar class reference in the Xcode documentation. Of particular interest would be the date(bySetting:value:of:) method, which will let you start from a given date and calculate a new date by changing the value of various date components.
You have a set of time intervals for each day. So you need a way to store, for a given day of the week, one or more time intervals. Your time intervals have a start time and an end time. Each of those needs to be represented by an hour, minute, and optionally second.
With that information you can get the current date/time and split it into components. Get the weekday, hour, minute, and second. Using the weekday you can get the appropriate time intervals. Then you can iterate those intervals and see if the current hour, minute, second falls between one of the intervals.
This all assumes that for a given business, your time intervals (open times) are specified in local time for the given business.
When converting the current date/time into its components, you should ensure that you set the calendar's timezone to match the timezone of the business in question.
There is no need for any date comparisons for any of this. You want to compare hours/minutes/seconds of the current date with the hours/minutes/seconds of the open times.
In my program, I have an NSArray of various dates and times, stored as strings and formatted like this: #[#"07:23",#"18:09",#"13:55"];
When I use an NSDateFormatter to convert these to NSDates, the times are correct, but year/month/day information is added.
The arrays that I have created are columns of a bus schedule. Each entry is one timeslot for whatever stop the array represents. My application needs to take the current time: [NSDate date] and see which time from the array is next in sequence. I'm just trying to display when the very next bus will arrive.
I have thought of comparing each element of the array with the current date and time using -[NSDate's laterDate:], but the problem is that when I convert the strings to NSDate objects, it gives them some random day-month-year like 13:55:00 January 1st, 2001 which will always be before the current date, so my test won't work.
I can find some workarounds that are really tragically McGuyvered but I would prefer something clean.
What I want to know are these things:
Can I remove the day/month/year portion from the NSDate?
Is it possible to easily set the day/month/year of each object in my array to today without using NSDateComponents and NSCalendar? I can manipulate them as they enter the array.
Would it be easier to reformat the current date/time to match the day/month/year of the array?
Otherwise, is there a better, cleaner solution to find the next upcoming timeslot? I am open to changing the entire format from arrays if necessary.
Can I remove the day/month/year portion from the NSDate?
No. An NSDate is merely an instant in time that is some number of seconds since some reference date. Describing that instant in time as some year/month/day depends on the local calendar. For example, the "day of month" of [NSDate date] as I type this is 28 where I live but 29 for the same NSDate value in Japan.
Is it possible to easily set the day/month/year of each object in my
NSMutableArray to today? without using NSDateComponents and
NSCalendar?
No. That's what NSDateComponents is for.
Otherwise, is there a better, cleaner solution to find
the next upcoming timeslot? I am open to changing the entire format
from arrays if necessary.
Use NSCalendar's -components:fromDate: to get an NSDateComponents object that matches [NSDate date]. Replace the hour/minute/second components with an arrival time's hour/minute/second: this is an arrival time today. Add one to the day component: this is an arrival time tomorrow. (Weekend and holiday schedules cause extra complication; the weekday component may be useful.) Convert back to NSDate using NSCalendar's -dateFromComponents: and perform your date comparisons there.
I want to store the date of an event in my database, but I want to do so without storing informations about seconds or anything smaller than seconds. Using Rails, in my migration I have the option to create a date column or a datetime column, the first one of which is too less accurate, and the second one is too much (up to the second and less). Which type should I choose to store such a date? Currently I'm using datetime and setting the seconds to a fixed value (e.g. 0) manually each time some date is set in the model.
Something like this:
self.date ||= Time.now.change(:sec => 0)
Am I totally out of track? Should I just use an integer field for each component of the date instead? (year, month, day, etc...) Or is datetime the correct type but I'm not understanding the purpose of it? (I think it's meant for timestamps and such things where seconds matter)
datetime is the correct type. And be sure to store it without time zone at time zone UTC:
http://derickrethans.nl/storing-date-time-in-database.html
At your option, use an SQL trigger to round your date to the minute on insert/update. It'll simplify your ruby code.
I want to let users specify a date that may or may not include a day and month (but will have at least the year.) The problem is when it is stored as a datetime in the DB; the missing day/month will be saved as default values and I'll lose the original format and meaning of the date.
My idea was to store the real format in a column as a string in addition to the datetime column. Then I could use the string column whenever I have to display the date and the datetime for everything else. The downside is an extra column for every date column in the table I want to display, and printing localized dates won't be as easy since I can't rely on the datetime value... I'll probably have to parse the string.
I'm hoping I've overlooked something and there might be an easier way.
(Note I'm using Rails if it matters for a solution.)
As proposed by Jhenzie, create a bitmask to show which parts of the date have been specified. 1 = Year, 2 = Month, 4 = Day, 8 = Hour (if you decide to get more specific) and then store that into another field.
The only way that I could think of doing it without requiring extra columns in your table would be to use jhenzie's method of using a bitmask, and then store that bitmask into the seconds part of your datetime column.
in your model only pay attention to the parts you care about. So you can store the entire date in your db, but you coalesce it before displaying it to the user.
The additional column could simple be used for specifying what part of the date time has been specified
1 = day
2 = month
4 = year
so 3 is day and month, 6 is month and year, 7 is all three. its a simple int at that point
If you store a string, don't partially reinvent ISO 8601 standard which covers the case you describe and more:
http://en.wikipedia.org/wiki/ISO_8601
Is it really necessary to store it as a datetime at all ? If not stored it as a string 2008 or 2008-8 or 2008-8-1 - split the string on hyphens when you pull it out and you're able to establish how specific the original input was
I'd probably store the datetime and an additional "precision" column to determine how to output it. For output, the precision column can map to a column that contains the corresponding formatting string ("YYYY-mm", etc) or it can contain the formatting string itself.
I don't know a lot about DB design, but I think a clean way to do it would be with boolean columns indicating if the user has input month and day (one column for each). Then, to save the given date, you would:
Store the date that the user input in a datetime column;
Set the boolean month column if the user has picked a month;
Set the boolean day column if the user has picked a day.
This way you know which parts of the datetime you can trust (i.e. what was input by the user).
Edit: it also would be much easier to understand than having an int field with cryptic values!
The informix database has this facility. When you define a date field you also specify a mask of the desired time & date attributes. Only these fields count when doing comparisons.
With varying levels of specificity, your best bet is to store them as simple nullable ints. Year, Month, Day. You can encapsulate the display logic in your presentation model or a Value Object in your domain.
Built-in time types represent an instant in time. You can use the built in types and create a column for precision (Year, Month, Day, Hour, Etc.) or you can create your own date structure and use nulls (or another invalid value) for empty portions.
For ruby at least - you could use this gem - partial-date
https://github.com/58bits/partial-date