Getting date from [NSDate date] off by a few hours

前端 未结 3 784
春和景丽
春和景丽 2020-11-21 07:36

I am using

NSDate *date = [NSDate date];

for getting the date, but the date I get is off by 2 hours.

3条回答
  •  萌比男神i
    2020-11-21 07:58

    NSDate objects don't have time zones. They represent an absolute moment in time. However, when you ask one for its description (by printing it with NSLog(), e.g.), it has to pick a time zone. The most reasonable "default" choice is GMT. If you're not in GMT yourself, the date will seem to be incorrect, by the amount of your own offset.

    You should always use an NSDateFormatter to create a string for display. The formatter's timezone should be set to yours, which is the default.

提交回复
热议问题