NSDate Date Doesn't Give Me The Right Hour

I am trying to get the current date with:

NSLog(@"DATE NOW : %@", [NSDate date]);

But when I execute it, I have a 1 hour difference between the displayed date and the current date.

Magazine http://data.imagup.com/12/1171458807.5637png

I found topics on such issues, but none of them gave me an answer. How to change this?

-2
source share
1 answer

You need to set the correct time zone. By default, as you can see, UTC is used.

You can use the date format for this.

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone * timezone = [NSTimeZone timeZoneWithName:@"Europe/Rome"];
[dateFormatter setTimeZone:timezone];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle
 ];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];    


NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);

For a list of available time zones, you can use

NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];

or check the constants on this Rails doc page .

+3
source

All Articles