Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting UTC date format to local nsdate

I am getting from my server a string date in UTC time zone and I need to convert it to the local time zone.

MY CODE:

let utcTime = "2015-04-01T11:42:00.269Z"     let dateFormatter = NSDateFormatter() dateFormatter.timeZone = NSTimeZone(name: "UTC") dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" let date = dateFormatter.dateFromString(utcTime) println("utc: \(utcTime), date: \(date)") 

this prints -

utc: 2015-04-01T11:42:00.269Z, date: Optional(2015-04-01 11:42:00 +0000)

if I remove

dateFormatter.timeZone = NSTimeZone(name: "UTC")  

it prints

utc: 2015-04-01T11:42:00.269Z, date: Optional(2015-04-01 08:42:00 +0000)

my local time zone is UTC +3 and in the first option I get UTC in the second option I get UTC -3

I should get

utc: 2015-04-01T11:42:00.269Z, date: Optional(2015-04-01 14:42:00 +0000)

So how do I convert the UTC date format to local time?

like image 894
ilan Avatar asked Apr 01 '15 14:04

ilan


People also ask

How do I convert UTC time to local in SQL?

SELECT CONVERT(datetime, SWITCHOFFSET(CONVERT(DATETIMEOFFSET, GETUTCDATE()), DATENAME(TZOFFSET, SYSDATETIMEOFFSET()))) AS LOCAL_IST; Here, the GETUTCDATE() function can be used to get the current date and time UTC. Using this query the UTC gets converted to local IST.

How do you convert UTC to local time in Swift?

I want to convert server UTC time to local time and vice-versa. Here is my code.. var isTimeFromServer = true var time:String! var period:String! let timeString = "6:59 AM" //Current UTC time if isTimeFromServer { let index = timeString.

What is current timestamp in UTC?

What is UTC Time in ISO-8601 Format? Current time: 19:51:38 UTC.


2 Answers

Something along the following worked for me in Objective-C :

// create dateFormatter with UTC time format NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];  [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; NSDate *date = [dateFormatter dateFromString:@"2015-04-01T11:42:00"]; // create date from string  // change to a readable time format and change to local time zone [dateFormatter setDateFormat:@"EEE, MMM d, yyyy - h:mm a"]; [dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; NSString *timestamp = [dateFormatter stringFromDate:date]; 

I keep these two websites handy for converting different time formats: http://www.w3.org/TR/NOTE-datetime

http://benscheirman.com/2010/06/dealing-with-dates-time-zones-in-objective-c/

In Swift it will be:

// create dateFormatter with UTC time format   let dateFormatter = DateFormatter()         dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"         dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone?         let date = dateFormatter.date(from: "2015-04-01T11:42:00")// create   date from string          // change to a readable time format and change to local time zone         dateFormatter.dateFormat = "EEE, MMM d, yyyy - h:mm a"         dateFormatter.timeZone = NSTimeZone.local         let timeStamp = dateFormatter.string(from: date!) 
like image 122
c_rath Avatar answered Sep 28 '22 08:09

c_rath


Try this Swift extension

Swift 4: UTC/GMT ⟺ Local (Current/System)

extension Date {      // Convert local time to UTC (or GMT)     func toGlobalTime() -> Date {         let timezone = TimeZone.current         let seconds = -TimeInterval(timezone.secondsFromGMT(for: self))         return Date(timeInterval: seconds, since: self)     }      // Convert UTC (or GMT) to local time     func toLocalTime() -> Date {         let timezone = TimeZone.current         let seconds = TimeInterval(timezone.secondsFromGMT(for: self))         return Date(timeInterval: seconds, since: self)     }  }   // Try it let utcDate = Date().toGlobalTime() let localDate = utcDate.toLocalTime()  print("utcDate - \(utcDate)")        //Print UTC Date print("localDate - \(localDate)")     //Print Local Date 
like image 32
Krunal Avatar answered Sep 28 '22 10:09

Krunal