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

前端 未结 3 746
春和景丽
春和景丽 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条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 08:14

    You can get your date corrected like this:

    NSDate * dateGMT = [NSDate date];
    NSTimeInterval secondsFromGMT = [[NSTimeZone localTimeZone] secondsFromGMT];
    NSDate * correctDate = [dateGMT dateByAddingTimeInterval:secondsFromGMT];
    

提交回复
热议问题