Convert unix timestamp to NSdate using local timezone

I get a few start_times and end_times in the form of NSDecimalNumber back from the API request.

I was able to successfully convert these NSDecimalNumber to NSDate s, but the code does not include time zones.

I need it to use the time zone that is installed by default on the device.

+7
source share
3 answers

This should do what you need with the 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]; 
+28
source

Unix time does not have a time zone . it is defined in UTC as the number of seconds since midnight on January 1, 1970.

You must get NSDates in the correct time zone using

 [NSDate dateWithTimeIntervalSince1970:myEpochTimestamp]; 
+9
source

Try something like this.

 NSDate* sourceDate = ... // your NSDate NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate]; NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate]; NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset; NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease]; 
+7
source

All Articles