iOS UIDatePicker time zone issue - iphone

I am having a very frustrating timezone issue with UIDatePicker.
When I initialise my date picker I set the time zone to the system time zone to allow the app to be used in multiple countries
[datePicker setTimeZone:[NSTimeZone systemTimeZone]];
Then a user selects a date. On saving the date (saved in CoreData) the date that comes back from the date picker is correct but then I want to just get the date and not the time i.e. with a time of 00:00:00
When I choose a date in GMT (not in BST) the date works fine. If I select date in BST (March - October) I get the date back as 1 hour behind.
This is how I am pulling the date out
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *dateOfBirth = [dateFormatter dateFromString:[dateFormatter stringFromDate: [dobDatePicker date]]];
I checked and the system time is definitly GMT, so then why does the value of dateOfBirth come back wrong
i.e
[dobDatePicker date] = "1991-05-30 18:58:16 +0000"
dateOfBirth = "1991-05-29 23:00:00 +0000"
Any suggestions? I dont want to force the timezone as I want it to run off the phones time zone.
Reason I am not interested in the time is I am storing this date as a date of birth and use it to calculate values like age and number of seconds until your birthday so dont want the current time from the date picker in the date I store in CoreData
Thanks

Use the following when fetching the time instead and let me know if it works for you.
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
This will return the phone's time zone always. I think systemTimeZone might be bugged. Cheers.

Related

What is the most reliable way to get the current UTC or zulu time regardless of user location or time settings?

Example: User local time is 10:30 pm PST in reality. Device can show 10:30 pm if user has internet and lets iOS automatically configure time and time zones. But some users disable this and do it by hand and get it wrong. For example no correct daylight saving time. I know from a friend who walked around with wrong time offset by 1 hour because of this.
I want to know the exact time in Zulu time or UTC-0 regardless of user location. What is the safest way to find out so my app can alert "NOW is Zulu 0" or "NOW is UTC-0" and it is correct. How?
You can set the time zone to UTC using
NSTimeZone *timeZoneUTC = [NSTimeZone timeZoneWithName:#"UTC"];
[NSTimeZone setDefaultTimeZone:timeZoneUTC];
and then get the current time using NSDate *today = [NSDate date];

Getting the date for the users current time zone?

When using NSDate to get today's date, I seem to be getting a date that is ahead in time. For example, today is February 19 but NSDate is giving me February 20?
What can I do to get the proper date?
I'm using NSDate *today = [NSDate date]; to get todays date but the problem is that it's not in my current time zone.
This sentence doesn't make sense. An NSDate object represents a single point in time, regardless of time zone. NSDate does not even have a sense of time zones.
To output a date as a string, always create an instance of NSDateFormatter and call its stringFromDate: method. By default, NSDateFormatter uses the current user's time zone.

Does [NSDate date] return the local date and time?

Am I stupid? All this time I thought [NSDate date] returned the local date and time. After having some trouble with NSStringformatter/stringFromdate/dateFromString today I noticed that my [NSDate date] was returning 2011-03-06 11:00:00 +0000. After researching this I see that [NSDate date] returns a raw date which is always GMT.
What purpose does the gmt offset portion serve if it always shows +0000? Also I do not understand [myDate description]. The docs says it is supposed to display gmt offset as well as dst information. I get the same thing as [NSDate date].
Bottom line for me, if I use [NSDate date] to get the current date and it is after 2pm I get tomorrow's date as I am in the -10 time zone. Not to mention the problems I ran into today with NSDateformatter.
Am I seeing this correctly? Funny thing is I seem to remember seeing [NSDate date] returning 2011-03-06 11:00:00 -36000, or did I think I was seeing 2011-03-06 11:00:00 -10000.
I can work with it, but maybe someone can expound on this to help me better understand NSDate.
NSDate returns the current date. Though it can convert to and from string format in different locales/time zones, NSDate has no internal concept of a time zone. NSDates are not bound to any particular region. If you and anyone else in the world asked your devices to [NSDate date] simultaneously, you'd get equal results — not because it always returns in GMT but because it doesn't return in a time zone-specific manner. In Cocoa, a date is a specific moment in the history of the Earth. How you write a date is related to your calendar, your time zone and your locale.
You aren't getting tomorrow's date, you're getting the correct date and then noticing it gives a different day if expressed in GMT. It's the correct date, just written differently from what you'd like.
'description' is a method overridden from NSObject. When you NSLog an object, what happens internally is that the description method is called and the string returned is printed. So you should get identical results from logging object and [object description], since the former calls description and prints that string, the latter calls description then calls description on the resulting string and prints that. NSStrings return themselves as the result of description.
It should be the default behaviour but to get a meaningful description, try:
NSLog(#"%#", [[NSDate date] descriptionWithCalendarFormat:nil
timeZone:[NSTimeZone localTimeZone] locale:[NSLocale currentLocale]]);
If that still logs in GMT then your device believes itself to be in GMT. I've no idea how reliable the simulator is about that sort of thing.
In Swift:
NSDate() gives us an agnostic date's description, it always returns the UTC+0 date time, independently of the device's time zone:
print(NSDate())
.descriptionWithLocale(NSLocale.currentLocale())! gives us a localized date's description UTC+N date time where N represents the offset (positive or negative) between UTC+0 and the current local date time:
print(NSDate().descriptionWithLocale(NSLocale.currentLocale())!)

Saving Timestamp into sqlite

18.11.2009 10:32:00
I want the value in between the above tag(created) to be inserted into the sqlite db for which i have taken a column of type timestamp....
how can i store this value in that column??
please advise...
Well, it depends in which format you want to save it in your database.
If you want to save it in the string form, then save it directly by making an object. But, use the datatype of string type.
Another option is to save it using the date datatype, seeing as sqlite doesn't have a dedicated date/time datatype.
Use a formatter to set the date format:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-YYYY HH:MM:SS"];
Then make Date object, and save it.
You should replace dots by hyphens and place months, days and year parts in correct order.
According to sqlite docs these are the date and time accepted formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
Two options here (if you want sorting, which I assume you do):
Add as a string, but re-order the fields from highest to lowest. After that, it's a simple matter to sort via text. This is the slower option.
2009.11.18 10:32:00
Convert to a UNIX timestamp, I.E. seconds since Jan 1st, 1970. This is the fastest option.
1261153920
It is then simple to pull it out using date and time functions.
Note that SQLite does not have a date/time data type.
Make object for date
then use this
[date timeIntervalSinceDate:objDate];
this give time interval beetween the date and objDate. two dates(date and objDate) for finding timeInterval.
Save this by convert it into string.

Getting number of days between [NSDate date] and string #"2010-11-12"

I need a function which receives [NSDate date] and a string #"2010-11-12" and returns the amount of days between those two dates.
More explanation:
I need to store a date from a server in the format #"2010-11-12" in my NSUserdefaults. The meaning of this date is the expireDate of a feature in an iPhone App. Every time I press on a button for this feature I need to check if the difference in days between the current time->[NSDate date] and #"2010-11-12" is greater than 0. That means that the feature is disabled.
It's making me crazy, maybe it's dead simple.
Use an NSDateFormatter to convert the string to an NSDate - then compare the date objects.