Reputation: 9994
I want to save a date object to the backend of my App. This is the code:
NSDate *date = self.birthPickerView.date;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *stringFromDate = [formatter stringFromDate:date];
NSLog(@"%@", stringFromDate);
NSDate *endDate = [formatter dateFromString:stringFromDate];
[formatter release];
NSLog(@"%@", endDate);
// Save to database
[user setObject:endDate forKey:@"birth"];
This is the print out result:
1985-03-05 00:00:00
1985-03-04 23:00:00 +0000
The end date is not right. I want to save 1985-03-05
in the database. Can you help me what is wrong?
Edit
[self.birthPickerView setTimeZone:[NSTimeZone localTimeZone]];
NSLog(@"%@", self.birthPickerView.date);
NSLog(@"%@", self.birthPickerView.timeZone);
[user setObject:self.birthPickerView.date forKey:self.navTitle];
This code save 1984-03-04 23:00:00
in database. what is wrong with it?
Upvotes: 0
Views: 379
Reputation: 4552
I don't see why would you need to convert your date into a string, and then back into the date again.
However, I'm pretty sure this happens because you didn't set the timezone on your NSDateFormatter. The default time zone is GMT which might cause the time offset you see there.
Upvotes: 2