NSDate 및 NSDateComponents가 잘못된 날짜를 반환함
-
12-12-2019 - |
문제
이 질문이 백만 번이나 제기되었다는 것을 알고 있지만 해결책을 찾을 수 없습니다.문제는:나는 수정하고있다 NSDate
이번 달의 첫 번째 날의 날짜 또는 다음 달의 첫 번째 날의 날짜를 반환합니다.한 가지를 제외하고는 잘 작동하는 것 같습니다.시간에서 2~3시간을 뺀 시간을 돌려줍니다.
암호:
NSCalendar *theCalendar = [NSCalendar currentCalendar];
int comp = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *theComp = [theCalendar components:comp fromDate:date];
[theComp setTimeZone:[NSTimeZone systemTimeZone]];
switch (roundingMode) {
case FIRST_OF_MONTH:
[theComp setDay:1];
return [theCalendar dateFromComponents:theComp];
case FIRST_OF_NEXT_MONTH:
[theComp setMonth:theComp.month+1];
[theComp setDay:1];
return [theCalendar dateFromComponents:theComp];
}
디버거(gdb) 출력은 다음과 같습니다.
(gdb) po theComp
<NSDateComponents: 0x6e26170>
TimeZone: Europe/Kiev (EET) offset 7200
Calendar Year: 2012
Month: 3
Day: 1
(gdb) po date
2012-03-12 13:05:22 +0000
(gdb) po [theCalendar dateFromComponents:theComp]
2012-02-29 22:00:00 +0000
어떤 도움이라도 대단히 감사하겠습니다.
감사합니다.
해결책
날짜는 GMT로 표시됩니다. 2012-02-29 00:00:00 +0000
~이다 2012-03-01 22:00:00 +0200
.또한 때때로 3시간의 차이가 발생하는 이유를 설명하는 일광 절약 시간 변경을 잊지 마십시오.
결과 날짜는 정확하지만 디버거는 이를 GMT로 표시합니다.
제휴하지 않습니다 StackOverflow