ios - Converting UTC date format to local nsdate -


i getting server string date in utc time zone , need convert 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 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 utc +3 , in first option utc in second option utc -3

i should get

utc: 2015-04-01t11:42:00.269z, date: optional(2015-04-01 14:42:00 +0000)

so how convert utc date format local time?

something along following worked me in objective-c :

// create dateformatter 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 string  // change readable time format , change local time zone [dateformatter setdateformat:@"eee, mmm d, yyyy - h:mm a"]; [dateformatter settimezone:[nstimezone localtimezone]]; nsstring *timestamp = [dateformatter stringfromdate:date]; 

i keep these 2 websites handy 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 be:

// create dateformatter utc time format let dateformatter = nsdateformatter() dateformatter.dateformat = "yyyy-mm-dd't'hh:mm:ss" dateformatter.timezone = nstimezone(name: "utc") let date = dateformatter.datefromstring("2015-04-01t11:42:00")// create   date string  // change readable time format , change local time zone dateformatter.dateformat = "eee, mmm d, yyyy - h:mm a" dateformatter.timezone = nstimezone.localtimezone() let timestamp = dateformatter.stringfromdate(date!) 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -