ios - NSString Date to NSDate -


this question has answer here:

nsstring date nsdate in ios not displaying proper details. have nsstring date :

nsstring *strfinal = @"2016-10-20 12:00:00"; 

i following below method convert nsstring nsdate

  nsstring *strfinal = @"2016-10-20 12:00:00";       nsdateformatter *dateformatter = [[nsdateformatter alloc] init];  //    [dateformatter setdateformat:@"yyyy-mm-dd hh:mm:ss"];      [dateformatter setdateformat:@"yyyy-mm-dd hh:mm:ss"];      nsdate *datefromstring = [[nsdate alloc] init];      datefromstring = [dateformatter datefromstring:strfinal]; 

and out put :

string date : 2016-10-20 12:00:00
nsdate is: 2016-10-19 18:30:00 +0000

it generates different date , different format nsstring want same nsdate.

any appreciated. thanks

you have problems time zone , can solved adding line of code:

[dateformatter settimezone:[nstimezone timezonewithname:@"gmt"]]; 

you can set timezone by:

[dateformatter settimezone:[nstimezone systemtimezone]];  or  [dateformatter settimezone:[nstimezone localtimezone]];  or  [dateformatter settimezone:[nstimezone timezonewithname:@"utc"]]; 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -