Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a unix timestamp to NSdate using local timezone

I'm getting some start_times and end_times in the form of NSDecimalNumbers back from an API request.

I have successfully been able to convert these NSDecimalNumbers into NSDates, but the code is not taking time zones into account.

I need for it to use the timezone that is default on the device.

like image 654
AlexanderN Avatar asked Jun 06 '12 21:06

AlexanderN


2 Answers

This should do what you need with current locale

double unixTimeStamp =1304245000;
NSTimeInterval _interval=unixTimeStamp;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
NSDateFormatter *formatter= [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateFormat:@"dd.MM.yyyy"];
NSString *dateString = [formatter stringFromDate:date];
like image 110
shabbirv Avatar answered Oct 05 '22 04:10

shabbirv


Unix time doesn't have a time zone. it's defined in UTC as the number of seconds since midnight Jan 1 1970.

You should get the NSDates in the correct time zone by using

[NSDate dateWithTimeIntervalSince1970:myEpochTimestamp];
like image 31
David Rönnqvist Avatar answered Oct 05 '22 04:10

David Rönnqvist