views:

322

answers:

2

i want to save current date+time into database and also want to retrieve it. i know that in objective-C we cannot save date+time directly. i have double as a field into database and currently I'm saving date by converting it into double.

here is the method for converting date into double-

currentDate=[NSDate date];

startDate=(double)[currentDate timeIntervalSince1970];

now how do i retrieve date back from that double value? or is there another way to save dates in iPhone SDK.

+2  A: 
NSDate *dateFromDouble(double interval) { 
  return [NSDate dateWithTimeIntervalSince1970: interval];
}
Louis Gerbarg
thanks for your answer.now please tell me that how do i convert it into hours,min,seconds.what i want to do is something like this.i started a timer counting down showing countdown on a label.the time when counter starts i save it into database.now i quit my app.now i want to show that time again on a label.how do i do this?
Rahul Vyas
A: 

Another alternative is to convert the date to ISO 8601 and save it as TEXT; this can be done using -description and -initWithString:. It will make the date slightly larger in the database; but it'll also be a lot more readable for debugging purposes.

Williham Totland