开发者

Storing NSDate with system time zone in Core Data

开发者 https://www.devze.com 2023-04-13 03:03 出处:网络
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.

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

精彩评论

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