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 fo开发者_开发技巧rmat (Time zone)
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];
NSDateFormatter is all about converting dates to and from string representations. Take a look at the docs for that.
精彩评论