获取每月的第一天[重复]

时间:2022-09-22 15:46:29

This question already has an answer here:

这个问题在这里已有答案:

I am trying to get an NSDate object from the first day of the current month. I use the following code:

我试图从当月的第一天获得一个NSDate对象。我使用以下代码:

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

    NSDate *dayOneInCurrentMonth = [c dateFromComponents:components];

However when I log this date, it gives me the day before

但是,当我记录这个日期时,它给了我前一天

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

1 个解决方案

#1


3  

You are logging the date in GMT time zone (+0000). Try logging this:

您正在以GMT时区(+0000)记录日期。尝试记录下来:

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

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

#1


3  

You are logging the date in GMT time zone (+0000). Try logging this:

您正在以GMT时区(+0000)记录日期。尝试记录下来:

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

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