Rupesh
Rupesh

Reputation: 7906

iPhone: convert Nsstring to Nsdate on device timezone

I received date from Web service which is in GMT + 1 format (2010-02-25T20:16:50.387+05:30). I want to convert in NSdate. I have no proper idea about date formatter.

Please suggest that how can i Change this nsstring value to current date format (Time zone)

Upvotes: 3

Views: 3548

Answers (2)

Hetal Vora
Hetal Vora

Reputation: 3361

Convert "2010-02-25T20:16" into NSDate using DateFormatter:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMdd'T'HHmm";
NSDate *gmtDate = [formatter dateFromString:@"2010-02-25T20:16"];

Once you have the GMT date, you can add/substract the GMT-your time zone offset to it to get the current NSDate in your time zone. To get the offset between your timezone and GMT, you can use the following:

NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];

Upvotes: 2

Chuck
Chuck

Reputation: 237010

NSDateFormatter is all about converting dates to and from string representations. Take a look at the docs for that.

Upvotes: 1

Related Questions