UIDatePicker gives the wrong time

Possible duplicate:
Discard the date [NSDate date] for a few hours
NSDate does not give me a suitable 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;

Basically, I get the date from UIDatePicker, set the hours and minutes as the label text, and save the date in a property called fromHour. In the method -viewDidLoad, I also have:

self.datePicker.timeZone = [NSTimeZone localTimeZone];

This is the conclusion:

enter image description here

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

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

Can you help me?

+1
1

, , , .

. NSLog , %@, description.

description NSDate , Zulu (+0000).

, , NSDateFormatter .

. , , NSDate. HH:mm. NSDate . - .

+4

All Articles