Creating NSDate from NSDateComponents off by one day

前端 未结 2 828
孤独总比滥情好
孤独总比滥情好 2021-01-25 14:32

I\'m trying to set a particular date for a unit test:

NSCalendar *calendar = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianC         


        
2条回答
  •  灰色年华
    2021-01-25 14:35

    NSDate doesn't have any timezone data associated with it. It is actually just a number of seconds from "the first instant of 1 January 2001, GMT." So the date components and calendar are creating an NSDate using your local timezone. That NSDate is the correct number of seconds from 1/1/2001 00:00 GMT, but when you log it, it's showing you the date in GMT instead of the local timezone. If you adjust that logged time to your timezone, it will be 2011-05-12 01:00:00.

提交回复
热议问题