开发者

Storing NSDate with system time zone in Core Data

When I store a date in CoreData as [NSDate date], it is saved -5:30 difference. In core data I used attribute type as date.

How to store NSdate with timeZone?

Update: Here is the code I am using:

To store the date:

database = (DataBase*) [fetchResults objectAtIndex:indexVal]; 
[database setDate:[NSDate date]]; 
NSError error = nil; 
[managedObjectContext save:&error] 
开发者_StackOverflow

To retrieve the date:

DataBase *newDataBase = (DataBase) [fetchResults objectAtIndex:i]; 
NSDate *RetrivedDate = [newDataBase Date]; 
NSLog(@"Retrived Date :",RetrivedDate"); 

Before storing I log it. It shows current date and time. After storing I immediately fetched the date . but it showed 1 day delayed date..


NSDate doesn't have a time zone. It stores dates as a number of seconds since a reference date in GMT.

The time zone is applied when you format the date for display using NSDateFormatter. This will pick up the device time zone by default.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜