Rohan Sanap
Rohan Sanap

Reputation: 2862

Store UTC date from server in NSDate object

I receive UTC date from server as 2015-10-07 17:43:11. I want to store this date in NSDate object. I do following code:

NSString *dateFromServer = @"2015-10-07 17:43:11";
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [[NSDate alloc] init];
date = [formatter dateFromString:dateFromServer];
NSLog(@"%@", date);

This gives me result as:

2015-10-07 12:13:11 +0000

(I stay in GMT+05:30 zone). How can I store my UTC time (which I receive from the server)directly into the NSDate object?

Upvotes: 2

Views: 155

Answers (4)

Yariv Nissim
Yariv Nissim

Reputation: 13343

There are 2 possible solutions:

1. Have the server send the time zone embedded in the date string:

"2015-10-07 17:43:11 UTC"

then parse it with the appropriate date format which will always give you the correct time zone.


2. Otherwise, you'll have to explicitly tell the formatter what is the time zone of the string or it'll use the current time zone of the device by default.

let dateFromServer = "2015-10-07 17:43:11"

let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
formatter.timeZone = NSTimeZone(abbreviation: "UTC")

if let date = formatter.dateFromString(dateFromServer) {
    print(date) // "2015-10-07 17:43:11"
}

Note: The NSDate object does not have a time zone - dates are universal and indicate a point in time. To display a date with a time zone you use a formatter to create a locale sensitive string.

Also there's no need to do this:

NSDate *date = [[NSDate alloc] init];

This creates a new date object with the current time. You're overriding this value in the next line.

Upvotes: 1

Alex
Alex

Reputation: 616

You don't need to covert anything, you only need to consider how to display date in UI, because 2015-10-07 12:13:11 +0000 and 2015-10-07 17:13:11 UTC + 5 are the same NSDate instances.

So if you want to get string representation of the NSDate instance retrieved from server in your local timezone, Just do this.


    NSDateFormatter * formatter = [NSDateFormatter new];
    formatter.timeStyle = NSDateFormatterStyleMediumStyle;
    formatter.dateStyle = NSDateFormatterStyleMediumStyle;
    ...

You can find more informations about how to format date/time in apple docs.

Upvotes: 0

Piyush
Piyush

Reputation: 1544

NSDate *currentDate = [[NSDate alloc] init];

Now it is in UTC, (at least after using the method below) To store this time as UTC (since refernce date 1970) use

double secsUtc1970 = [[NSDate date]timeIntervalSince1970];

Set Date formatter to output local time:

NSTimeZone *timeZone = [NSTimeZone defaultTimeZone];

// or specifc Timezone: with name

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString *localDateString = [dateFormatter stringFromDate:currentDate];

A NSDate object always uses UTC as time reference, but the string representation of a date is not neccessarily based on UTC timezone.

Please note that UTC is not (only) a timeZone, It is a system how time on earth is measured, how it is coordinated (The C in UTC stands for coordinated). The NSDate is related to a reference Date of midnight 1.1.1970 UTC, altough slightly wrongly described by Apple as 1.1.1970 GMT.

Upvotes: 0

Rohan
Rohan

Reputation: 2935

Try like this ;

NSString *dateFromServer = @"2015-10-07 17:43:11";
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
NSDate *date = [[NSDate alloc] init]; 
date = [formatter dateFromString:model.start_time]; 
NSLog(@"%@", date);

Upvotes: 0

Related Questions