I am using Core-Data
to store date-time object.
Eg. 2015-07-28T07:16:52+0000
this is date in ISO format in GMT
timezone.
But when I save this date in database
NSString* dateString=@"2015-07-28T07:16:52+0000";
NSDateFormatter* dateTimeformatter=[[NSDateFormatter alloc] init];
[dateTimeformatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
[dateTimeformatter dateFromString:dateString];
the resulting date which I see in database is 2015-07-28 12:46:52
which is in IST
according to my device's timezone
I tried to set timezone as well in dateFormatter but again the same response
NSString* dateString=@"2015-07-28T07:16:52+0000";
NSDateFormatter* dateTimeformatter=[[NSDateFormatter alloc] init];
[dateTimeformatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
[dateTimeformatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
[dateTimeformatter dateFromString:dateString];
Even when I save the [NSDate date]
directly in database , it saves the converted date according to device's timezone.
Why Core-data is not taking the timezone of NSDateFormatter into account?
Can anyone tell me how can I save the date in GMT
in database irrespective of my device's timezone?
I managed to solve problem I was facing by doing some research and using zpasternack's answer .I mentioned some findings for anyone who may face such problems related to timezone.
Findings:
The date which is saved in database
(originally in any timezone) will be saved in 'GMT/UTC'
timezone.
The date which is seen in local timezone
in database directly is because of third party tool
which is used to see the data in database.
When date is taken out of database
, then also it is in 'GMT/UTC'
timezone.
To see the date in specific timezone
, the NSDateFormatter
is used with the specific timezone specified.
The most important is NSDate
object is always irrespective of any timezone
and is always in 'GMT/UTC'