سؤال

I have a string like a below example

2013-12-28T8:15:00+03:00

but i want to convert to NSDate with same format here is my code

NSDateFormatter *dateFormatterInBoundSegment = [[NSDateFormatter alloc] init];

[dateFormatterInBoundSegment setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssz"];

[dateFormatterInBoundSegment setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

NSDate *dateInboundSegment = [dateFormatterInBoundSegment dateFromString:@"2013-12-28T8:15:00+03:00"];

NSLog(@"Formatted date %@",dateInboundSegment); 

And result printed like this

Formatted date 2013-12-28 05:15:00 +0000

I want to convert to nsdate with GMT +3 like string ,result should be like this

2013-12-28T8:15:00+03:00
هل كانت مفيدة؟

المحلول

NSDate doesn't store the time zone, NSDate is just a number that represents a date. When you print it on the screen, it's just formatted like that, but your date and that one are the same.

This:

2013-12-28 05:15:00 +0000

and this:

2013-12-28T8:15:00+03:00

Represent the same date, just printed in a different way.

When you do this: NSLog(@"Formatted date %@",dateInboundSegment); You are not specifying how the date has to be formatted to string, so it's using a default mode.

If you want to print the NSDate with the proper timezone, you need to create a string with a formatter and set the timezone.

نصائح أخرى

You are printing the NSDate value, not the formatted string. NSDate doesn't hold any format, and it's printed using the default format.

In order to print a string with your desired format, you must call:

[dateFormatterInBoundSegment stringFromDate:myDate];

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top