Wednesday, 21 August 2013

Formatting Date from server time to local time using NSTimeZone and Dateformatter

Formatting Date from server time to local time using NSTimeZone and
Dateformatter

I am receiving a date/time as a NSString from my server where I am
converting that time into a NSDate to the users local time using
NSTimeZone. After which I try to reformat this NSDate into a better more
readable NSString using the new NSDateFormatter format, however when I try
to apply this new format it reverts the resulting dateString back to the
original Server time.
I would like to know what I am doing wrong, I would like to show the
converted time in the new format.
this is the code I am using
// set date format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// change time to systemTimeZone
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:timeZone];
NSDate *localTime = [dateFormatter
dateFromString:[singleInstanceActivationHistoryDictionay
objectForKey:@"ActivationTime"]];
// reformat converted Time to readable format
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:@"dd/MM/yy - hh:mm a"];
NSString *dateWithNewFormat = [dateFormat1 stringFromDate:localTime];
NSLog(@"TimeZone - %@", timeZone);
NSLog(@"UTC ServerTime - %@",
[singleInstanceActivationHistoryDictionay
objectForKey:@"ActivationTime"]);
NSLog(@"UTC to deviceTimeZone - %@", localTime);
NSLog(@"NewFormat - %@", dateWithNewFormat);
This is an example of my output
TimeZone - Pacific/Auckland (NZST) offset 43200
UTC ServerTime - 2013-08-22 01:45:59
UTC to deviceTimeZone - 2013-08-21 13:45:59 +0000
NewFormat - 22/08/13 - 01:45 AM
any help would be greatly appreciated

No comments:

Post a Comment