Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert UTC date string to local time (systemTimeZone)

Input String: June 14, 2012 - 01:00:00 UTC

Output Local String: Jun 13, 2012 - 21:00:00 EDT

I like to get the offset from

NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSLog(@"Time Zone: %@", destinationTimeZone.abbreviation);

Any suggestion ?

like image 474
AAV Avatar asked Jul 26 '12 20:07

AAV


People also ask

How do I convert UTC timestamp to local time?

(GMT-5:00) Eastern Time (US & Canada) Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.

How do you convert UTC time to local time in node JS?

Use the Date() constructor to convert UTC to local time, e.g. new Date(utcDateStr) . Passing a date and time string in ISO 8601 format to the Date() constructor converts the UTC date and time to local time. Copied!

How do I convert DateTime to local time?

To convert the time in any designated time zone to local time, use the TimeZoneInfo. ConvertTime method. The value returned by the conversion is a DateTime whose Kind property always returns Local. Consequently, a valid result is returned even if ToLocalTime is applied repeatedly to the same DateTime.

What is UTC time string?

Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z"). Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC.


1 Answers

This should do what you need:

NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"LLL d, yyyy - HH:mm:ss zzz";
NSDate *utc = [fmt dateFromString:@"June 14, 2012 - 01:00:00 UTC"];
fmt.timeZone = [NSTimeZone systemTimeZone];
NSString *local = [fmt stringFromDate:utc];
NSLog(@"%@", local);

Note that your example is incorrect: when it's 1 AM on June-14th in UTC, it's still June-13th in EST, 8 PM standard or 9 PM daylight savings time. On my system this program prints

Jun 13, 2012 - 21:00:00 EDT
like image 138
Sergey Kalinichenko Avatar answered Sep 28 '22 03:09

Sergey Kalinichenko