2013-10-20 167 views
0

我想從本月的第一天獲取NSDate對象。我使用下面的代碼:獲取月的第一天

NSDateComponents *components = [c components:(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekdayOrdinalCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]]; 
    components.day = 1; 

    NSDate *dayOneInCurrentMonth = [c dateFromComponents:components]; 

然而,當我登錄這個日期,它給了我前一天

day one = 2013-09-30 22:00:00 +0000 
+2

是您的時區UTC -2?在這種情況下,dayOneInCurrentMonth是正確的日期,但NSLog輸出它與此時區更正 – Jeepston

+0

@Jeepston你和喬爾是正確的。這是時區。 –

回答

3

要登錄GMT時區的日期(+0000)。嘗試日誌記錄:

NSLog(@「new date:%@」,[dayOneInCurrentMonth descriptionWithLocale:[NSLocale systemLocale]]);

相關問題