1

Possible Duplicate:
Getting date from [NSDate date] off by a few hours
NSDate date don’t give me the right hour

I have this piece of code:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];

NSString *formattedDateInString = [formatter stringFromDate:[self.datePicker date]];
NSLog(@"This is the formatted date (STRING) ---> %@" ,formattedDateInString);
self.fromHourLabel.text = formattedDateInString;

NSDate *date = [formatter dateFromString:formattedDateInString];
NSLog(@"This is the date (NSDate) ---> %@" ,date);
self.fromHour = date;

I basically get the date from a UIDatePicker, set Hours and Minutes as a label text and save the date into a property called fromHour. In the -viewDidLoad method I also have:

self.datePicker.timeZone = [NSTimeZone localTimeZone];

This is the output:

enter image description here

as you can see, with this code, I have two main problems:

  • The first output say 17.30 and the second 16:30 (the first output is correct, the second is wrong) why?
  • The second output is formatted with "yyyy-MM-dd HH:mm:ss" but I need only "HH:mm".

Can you help me?

Community
  • 1
  • 1
Marco Manzoni
  • 707
  • 3
  • 11
  • 21
  • Are you trying to save the whole date in fromHour, or just the time? You say you're saving a date, but the way you do it only gives you time (and an incorrect one at that). – rdelmar Jan 27 '13 at 18:14
  • 3
    This has got to be the second most-asked Cooca question here. Please search before asking. – jscs Jan 27 '13 at 20:08

1 Answers1

4

I know there are dozens of existing questions that cover this but I can't find one.

The problem is simple. When you print an object using NSLog or a string format containing the %@ format specifier, the description method is called on the object.

The description method of NSDate always displays the full date using the Zulu timezone (+0000).

If you want a date printed in a specific format then use NSDateFormatter to convert to a string and print the string.

Edit: Based on info from the comments, part of the problem here is that you start with the proper NSDate. You then convert that to a string with the format HH:mm. You then create a new NSDate from that string. The better solution is to simply save the original date and avoid the creation of the 2nd date.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • In this case I need to set the property self.fromHour to a NSDate formatted with this style: "HH:mm". The NSLog is intended only to show the output. How can I set the self.fromHour property with the correct 17.30 NSDate? – Marco Manzoni Jan 27 '13 at 18:04
  • Right now your property is an `NSDate`. `NSDate` objects are not formatted themselves. Either you use an `NSDateFormatter` every time you wish to display the date (which is fine), or you change the property to an `NSString` containing the pre-formatted value. – rmaddy Jan 27 '13 at 18:07
  • So there is no way to "save" an hour as NSDate (and not NSString) without encountering this problem? I need that my property is NSDate and not NSString and that the time is correctly saved (In this case, 17.30 and not 16.30). – Marco Manzoni Jan 27 '13 at 18:13
  • 1
    There is no problem here. The NSDate object has the desired value. You just need to format it properly when needed, just like you did in the first part of the code you posted. Though instead of creating a new date from the string, like you did in the 2nd part of the code, simply save the original date from the picker. – rmaddy Jan 27 '13 at 18:16
  • No, his time is off by an hour in the NSDate object. – rdelmar Jan 27 '13 at 18:24
  • 1
    @rdelmar He went from `NSDate` to a string with just HH:mm, and then from that to a new `NSDate`. This is why I mentioned in the comment that he should just keep the original date and not the new date. – rmaddy Jan 27 '13 at 18:25
  • @mutexmark I updated my answer with more details from our comments. – rmaddy Jan 27 '13 at 18:28
  • Ok, I didn't realize that the description method always gave the time in Zulu timezone even when the date object contains the correct time. – rdelmar Jan 27 '13 at 18:30