1
votes

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] 

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..

1

1 Answers

1
votes

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.