0

So I have 2 entities which both carry an NSDate attribute. My "OwedMoney" entity has a "youOweDate" NSDate attribute and my "TheyOweMoney" entity has a "dueDate" NSDate attribute. On both View Controllers, I have a UIDatePicker type Date. So it only displays the month, day, and the year. Below in the following code is where I try to save the DatePicker's date value. My DatePicker's name is "personWhoOwesYouDueDate"

    NSDate *choice = [personWhoOwesYouDueDate date];

NSManagedObjectContext *context =  [self managedObjectContext];
TheyOweMoney *owedMoney = [NSEntityDescription insertNewObjectForEntityForName:@"TheyOweMoney" inManagedObjectContext:context];
[owedMoney setValue:choice forKey:@"dueDate"];

Then I have an NSLog to check the value

NSLog(@"%@", choice);

So when my application loads and I pick a date like February 8 2014 for example, I don't get the expected result

Entered: February 8 2014

Result: 2014-02-09 01:50:29 +0000

So it has the Year and month correct but the day is wrong. Also, is there a way to get rid of the "+0000" part? It's really annoying. Another thing is that my date value CANNOT be a string because when I set it's value to the attribute it MUST be an instance of NSDate.

All help is appreciated, thanks.

doc92606
  • 691
  • 2
  • 11
  • 32
  • This is just the way that dates are logged -- they show the date in GMT. The date that's stored will be the correct date based on your timezone. If you want to log (or display to the user) the date correctly, convert it to a string first, and log or display that. – rdelmar Feb 04 '14 at 02:10
  • So my date being a day off is normal? – doc92606 Feb 04 '14 at 02:12
  • The problem is when i convert it to a string I can't set the value of my attribute to it. My attribute only take an NSDate. – doc92606 Feb 04 '14 at 02:13
  • It's normal for it to be off by the difference between your local time and GMT -- if that causes it to be in a different day, then that's what will be logged. You only convert it to a string for display or logging purposes -- you still add it to your entity as a date. – rdelmar Feb 04 '14 at 02:14
  • Ahhhhhh I see you. It's been a long frustrating day bro, I'm sorry. Sigabrts are so hurtful to my self esteem, lol. Thanks! – doc92606 Feb 04 '14 at 02:16
  • 2
    possible duplicate of [Getting date from \[NSDate date\] off by a few hours](http://stackoverflow.com/questions/8466744/getting-date-from-nsdate-date-off-by-a-few-hours) – jscs Feb 04 '14 at 02:21

1 Answers1

0

If UIDatePicker's mode is UIDatePickerModeDate it lets you select day, month and year information but a NSDate also has a time component. So [personWhoOwesYouDueDate date] will give you a NSDate consisting of the selected components and the time component of your current time (01:50:29 in this case). If you want 2014-02-08 00:00:00 to be stored do the following:

NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
cal.timeZone = [NSTimeZone systemTimeZone];
cal.locale = [NSLocale currentLocale];
NSDateComponents *dateComponents = [cal components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate: [personWhoOwesYouDueDate date]];
[owedMoney setValue:[cal dateFromComponents: dateComponents] forKey:@"dueDate"];

@rdelmar: NSDate does NOT store any timezon information. So it is not based on any timezone. Timezone just comes into play when logging the NSDate which will display the GMT based dated "shifted" to the timezone set.

RTasche
  • 2,614
  • 1
  • 15
  • 19