init] returning date an hour in the past?

不想你离开。 提交于 2019-12-19 11:29:15

问题


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


回答1:


Use NSDateFormatter to localize the date:

NSLog(@"%@",[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]); 



回答2:


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)




回答3:


Use NSDate *now = [NSDate date];

I am using it and giving me perfect result.




回答4:


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



来源:https://stackoverflow.com/questions/3889929/init-returning-date-an-hour-in-the-past

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!