0
votes

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

1
What timezone is the date that your server gives you? You need to set your first NSDateFormatter's time zone to the time zone of the server so that the NSDateFormatter will interpret the string in the server's timezone. That way, when you get the resulting NSDate it will represent the correct point in time (note that NSDate objects are oblivious to timezones, they represent specific moments in time regardless of timezones and daylight saving etc). When you have correctly parsed the "specific moment", you can represent that "specific moment" in local time by using your second date formatterdreamlax
okay.. so the time zone my server is set to is UTCHurkNburkS
that worked. I set the dateFormatter to UTC then dateFormatter1 got set to timeZone.. worked perfectly :) I just need to change var names to represent my data abit better. :) thanks for the helpHurkNburkS
An NSDate object contains (or should contain, if properly created) the GMT/UTC value of the date/time. Getting the time in your local timezone is the job of the date formatter. You do not modify the NSDate object to set the timezone.Hot Licks

1 Answers

0
votes

The NSDateFormatter that reads the date must be set to the timezone that the date you are parsing is in, in your case, it is UTC. The date formatter will then be able to produce an NSDate object (which represents a specific moment in time regardless of timezones). You can then give that NSDate object to another NSDateFormatter that is configured to format dates in a specific time zone.

// set date format
NSDateFormatter *dateParser = [[NSDateFormatter alloc] init];
dateParser.dateFormat = @"yyyy-MM-dd HH:mm:ss";
dateParser.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
NSDate *specificMomentInTime = [dateParser dateFromString:[singleInstanceActivationHistoryDictionay objectForKey:@"ActivationTime"]];

// reformat converted Time to readable format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd/MM/yy - hh:mm a";
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:specificMomentInTime];

NSLog(@"UTC ServerTime - %@", specificMomentInTime);
NSLog(@"NewFormat - %@", dateWithNewFormat);