1

When I call

NSDate *now = [[NSDate alloc] init]; 

in order to get the current date and time, I check it with:

NSLog(@"Date now: %@", now);

the date outputted is one hour in the past.

2010-10-08 12:04:38.227 MiniBf[1326:207] Now: 2010-10-08 11:04:38 GMT

Is my time zone set incorrectly somewhere perhaps?

Thanks!

Michael

Smikey
  • 8,106
  • 3
  • 46
  • 74
  • Are you testing with simulator or real device? Is summer time (daylight saving time) active? Please provide more information. – AlexVogel Oct 08 '10 at 11:31
  • I'm testing it using the simulator. I can't find anywhere in the simulator to disable daylight saving? – Smikey Oct 08 '10 at 14:10

4 Answers4

1

I've seen that behavior on the DatePicker running on iOS 4.1 devices while on iOS 4.0 and previous it does not happen. I can't confirm it is actually the same bug.

But I suggest you check whether is related with the acknowledged by Apple bug regarding dates/summer time/4.1 by running that code on < 4.1. Then you will need to decide whether to hack a fix for that particular version/dates or wait for the next release of the SDK to fix it and not support 4.1 devices...

The bug I'm talking about I believe only happens while passing dates within the summer time period (2010: 28th March - 31st October)

Juan Fran Jimenez
  • 1,629
  • 15
  • 19
0

Use NSDate *now = [NSDate date];

I am using it and giving me perfect result.

Javal Nanda
  • 1,828
  • 3
  • 14
  • 26
  • this method uses the -init method, ie you can read it as [[[NSDate alloc] init] autorelease]; There should be no difference. – Matthias Bauch Oct 08 '10 at 13:03
0

Maybe your timezone is wrong in your iPhone simulator. Press the home button, go to settings.app and correct it ;)

Saa
  • 1,540
  • 10
  • 22
  • I can't find anywhere in the settings to change the time. The time on the status bar is also correct. – Smikey Oct 08 '10 at 14:11
0

Use NSDateFormatter to localize the date:

NSLog(@"%@",[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]); 
Saa
  • 1,540
  • 10
  • 22
  • That does indeed display the correct date. However, the date when initialised as above is still wrong by an hour... :o – Smikey Oct 11 '10 at 17:51
  • 1
    NSDate works in GMT "The sole primitive method of NSDate, timeIntervalSinceReferenceDate, provides the basis for all the other methods in the NSDate interface. This method returns a time value relative to an absolute reference date—the first instant of 1 January 2001, GMT." – Saa Oct 12 '10 at 08:53