I'm getting some strange output from a NSDateFormatter. I'm converting a date from GMT to the system time zone, which is EDT. This should be -4 hours from GMT.
-(NSDate*)parseDate:(NSString*)inStrDate {
NSLog(#"Date To Parse %#", inStrDate);
NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setLocale:[NSLocale systemLocale]];
[dtFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dtFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss +0000"];
NSDate* dateOutput = [dtFormatter dateFromString:inStrDate];
NSLog(#"Parsed Date %# %#", dateOutput, [NSTimeZone systemTimeZone]);
return dateOutput;
}
Log output:
2012-09-15 22:32:53.358 Date To Parse 2012-09-16 02:32:53 +0000
2012-09-15 22:32:53.360 Parsed Date 2012-09-16 06:32:53 +0000 America/New_York (EDT) offset -14400 (Daylight)
But it's returning +4 hours instead of -4. So where it should output 22.30 EDT (02.30 GMT) it's actually returning 06.30 EDT. Which is 8 hours in the future.
Can anyone help me understand if I'm going wrong somewhere here? I'm scratching my head but I can't figure out why this wont seem to work.
Thanks
You just got the dateFormatter the wrong way around.
When you do
[dtFormatter setTimeZone:[NSTimeZone systemTimeZone]];
you're telling it that the input Date is in terms of your time zone. Therefore, it converts it to GMT and outputs the time +4 hours. To do it the other way around you can use this:
NSDateFormatter* df_local = [[NSDateFormatter alloc] init];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"America/New_York"]];
[df_local setDateFormat:#"yyyy.MM.dd' 'HH:mm:ss zzz"];
NSDate* dateOutput = [dtFormatter dateFromString:dateString];
NSLog(#"Parsed Date in GMT %#", dateOutput);
NSString *yourLocalDate = [df_local stringFromDate:dateOutput];
NSLog(#"in EDT %#",yourLocalDate);
This logs
2012-09-16 05:23:53.297 TestingApplication[10109:c07] Date To Parse 2012-09-16 02:32:53 +0000
2012-09-16 05:23:53.302 TestingApplication[10109:c07] Parsed Date in GMT 2012-09-16 02:32:53 +0000
2012-09-16 05:23:53.304 TestingApplication[10109:c07] in EDT 2012.09.15 22:32:53 EDT
if you want a date as a final output you can skip the secondary NSDateFormatter and just 'add' the difference between the UTC date and the local time.
formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [formatter dateFromString:dateString]; // UTC date
NSDate *date2 = [date dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMTForDate:date]]; // local date!
Related
I am using following code to get date from string. All seems to be good in code but while I print the output date, there is difference of 12:30 hours in date.
What may be the issue? Am I missing something ?
NSString *strDate = #"8/22/2017 7:00:00 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
NSDate *date = [df dateFromString:strDate];
NSLog(#"%#", [date description]);
Output:
2017-08-21 18:30:00 +0000
The hour specifier is wrong, 12 hour format is hh
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
Note:
Be aware that NSLog prints the date always in UTC although the date formatter considers the local time zone.
try this:
you just set the time to GMT
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
I have a NSString like this."10/28/2014 9:20 PM PDT".I have to convert exactly this to NSDate.But on conversion,the time zone is converted to IST.This is what i tried.
departTime=#"10/28/2014 9:20 PM PDT"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm a zzz"];
NSDate *departTimedate = [dateFormatter dateFromString:departTime];
This is what i am getting on converting to NSDate: "2014-10-29 04:40:00 +0000"
Can anyone help me on this.
Hi I am trying to display GMT time to my device time.
my device time zone is EDT.
I got the GMT time as a string
// GMT Time-----2014-11-27 19:32:00
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"HH:mm:ss";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *startDate = [dateFormatter dateFromString:#"2014-11-27 19:32:00"];
I am getting this start date as -
//startDate---2014-11-27 19:32:00 +0000
Now I am trying to convert this into my local time
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
dateFormatter2.dateFormat = #"hh:mm a";
NSTimeZone *local = [NSTimeZone localTimeZone];
[dateFormatter2 setTimeZone:[NSTimeZone localTimeZone]];
NSString *startDateStr = [dateFormatter2 stringFromDate:startDate];
As per my timezone(EDT) the result must be - 3:32 PM
But I am getting wrong time as
//startDateStr---02:32 PM
I have printed my timezone difference with GMT
NSLog(#"TimeZone %d",[[NSTimeZone localTimeZone] secondsFromGMT]);
// TimeZone -14400 - this correct and as per this the startDate must be 03:32 PM
Where I am making the mistake ?
Please help me
Very clever. The date that you converted was on January 1st 2000. So daylight savings time was different back then. 19:32 GMT on the 1st of January was 2:32pm in your time zone. But 19:32 GMT today is 3:32pm in your time zone.
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]];
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.)