How to I get UTC from NSDateFormatter? - ios

The following code in iOS
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterLongStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation: #"UTC"]];
NSString *dateString = [formatter stringFromDate: date];
returns a date string like
Monday, May 6, 2013, 10:42:10 PM GMT
Unfortunately, UTC is not exactly the same at GMT--it's off by one hour when the UK is on daylight savings time. How to I convince NSDateFormatter to actually return UTC rather than GMT?

According to this post, the two are implemented the same in code. I don't believe you need to worry about it.

Related

How does NSDate seem to know it's TimeZone?

I'm somewhat confused about how these two NSDate objects seem to know which timezone they are in. I was under the impression that an NSDate object only stored a point in time and no information about the timezone.
I'm creating and logging two dates like this:
NSString* timeString = #"6:04 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"h:mm a"];
NSDate *time = [dateFormatter dateFromString:timeString];
NSDateFormatter *currentFormatter = [[NSDateFormatter alloc] init];
[currentFormatter setTimeStyle:NSDateFormatterLongStyle];
NSLog(#"%#", [currentFormatter stringFromDate:time]);
NSLog(#"%#", [currentFormatter stringFromDate:[NSDate date]]);
This produces the output:
18:04:00 GMT
11:12:36 BST
How does it know that the first date is GMT and the second is BST?
(It's British Summer Time here. Your mileage may vary in that respect)
You're right, NSDate doesn't have a time zone. Your results don't contradict that, because you're not printing the dates-- you're printing a subset of the date information produced by passing them through a date formatter. Your currentFormatter only returns time of day information, not date information. If you add this line:
[currentFormatter setDateStyle:NSDateFormatterLongStyle];
Then the results will look something like:
January 1, 2000 at 6:04:00 PM MST
September 30, 2015 at 11:14:39 AM MDT
In other words, they show up with different time zones because they're on different dates, and the time zone reflects what's in effect on that date. In my case it's currently MDT but on Jan 1 2000 it would have been MST.

Convert NSString 8-8-2015 12:00:00 AM to NSDate

I am aware that this question is asked too many times but none matched my requirement.
I have "8-8-2015 12:00:00 AM" in NSString named strEventTimeBegin. How do I convert this NSString to NSDate? Here's what I tried but returns null.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSDate *startEventDate = [dateFormatter dateFromString:strEventTimeBegin];
From memory, but this should work:
[dateFormatter setDateFormat:#"d-M-yyyy hh:mm:ss a"];
Just to explain, in your example;
The day and the year are backwards.
You're using 2 digit days and
months (It's looking for 08-08-2015).
You're missing the period
(AM/PM) identifier.

iOS NSCalendar NSTimezone daylight saving time confusion [duplicate]

I am trying to convert the following string into an NSDate object:
NSString *str=#"25 May 2012 10:25:00";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"d MMM yyyy HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"asia/kolkata"]];
NSDate *date = [dateFormatter dateFromString:str];
In console : date-->2012-05-25 04:55:00 +0000....it lags behind 5 hours and 30 minutes and assumes GMT timezone instead of Asia...Why it is so?
When you see an [NSDate description] printed in the console, it is always the corresponding time in GMT. If you use the same date formatter to convert the date back to a string, it should be in the specified time zone.
An [NSDate description] is what you see if you type
po date
or
po [date description]
or you use NSLog to send either one of these forms to the console.
if you are looking for India Timezone you should use:

Remove time zone offset from date in iOS

I need to get the current NSDate.date and remove the time zone and parse it as a GMT date
NSDate.date
returns 2012-10-11 11:27:09 -0700
What I need is this: 2012-10-11 11:27:09 +0000
NSDate.date returns a date with the current date and time stored as GMT.
If you want to format the date as a string and show the GMT time, you should use a NSDateFormatter and set the locale to GMT:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"GMT"]];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *dateAsString = [formatter stringFromDate:[NSDate date]];

NSDateFormatter giving me time 4 hours ahead

I'm converting a string "Jun 11, 2012 9:30 PM" to an NSDate and I keep getting 4 hours ahead for some reason. The funny thing is I'm using this same string to feed a UIDatePicker in a detailed view where I have to do the same conversion, and the UIDatePicker renders the time fine. Only when I now try to NSLog it in my main and detailed view do I have problems.
This is in my views :
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMMM dd, yyyy h:mma"];
NSDate *dateFromString = [[[NSDate alloc] init]autorelease];
NSLog(#"DATE %#", _date);
dateFromString = [formatter dateFromString:_date];
NSLog(#"NSDATEFROMSTRING %#", dateFromString);
NSLog returns :
2012-06-11 00:02:09.136 LocalDeals[78090:207] DATE Jun 11, 2012 9:30 PM
2012-06-11 00:02:09.137 LocalDeals[78090:207] NSDATEFROMSTRING 2012-06-12 01:30:00 +0000
Even when I add:
[formatter setTimeZone:[NSTimeZone defaultTimeZone]];
I still get the same results from any time I choose. Any ideas? This is on the simulator by the way and yes my Region Format is set to United States.
When you NSLog an NSDate it will print the time as a GMT time zone
In order to see the correct data you will have to convert the NSDate to string using stringFromDate
NSDate *dateFromString = [[[NSDate alloc] init]autorelease];
NSLog(#"DATE %#", _date);
//Instead of nslog directly, use this stringFromDate:remindOn
NSString *str = [formatter stringFromDate:dateFromString];
NSLog(#"date is %#", str); //This will log the correct data
The problem you are getting is not in the NSDate but it is in Logging it
UPDATE In order to save the data to a file or database i would suggest that you save it like this
NSTimeInterval timeInterval = [dateFromString timeIntervalSince1970];
Now when you read it again from the database you would do
NSDate *data = [[NSDate alloc] initWithTimeIntervalSince1970:timeInterval]
The following code will show your time with your time zone:
NSString *_date = #"Jun 11, 2012 9:30 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMMM dd, yyyy h:mma"];
[formatter2 setDateFormat:#"MMMM dd, yyyy h:mma Z"];
NSDate *dateFromString = [[NSDate alloc] init];
NSLog(#"DATE %#", _date);
dateFromString = [formatter dateFromString:_date];
NSLog(#"NSDATEFROMSTRING %#", dateFromString);
NSLog(#"NSDATEFROMSTRING %#", [formatter stringFromDate:dateFromString]);
NSLog(#"NSDATEFROMSTRING %#", [formatter2 stringFromDate:dateFromString]);
Result:
DATE Jun 11, 2012 9:30 PM
NSDATEFROMSTRING 2012-06-12 04:30:00 +0000
NSDATEFROMSTRING June 11, 2012 9:30PM
NSDATEFROMSTRING June 11, 2012 9:30PM -0700
If you Google for UTC Time now, it does give you a time that is close to the second line in the output, which confirms it is printing out the time as a UTC time.
(We are in different time zone and dateFromString is interpreting the time in the string as your local time.)

Resources