简体   繁体   中英

How to get actual first day of the month, possible with my time zone?

I'm trying to get the first day of the month, but I'm getting the UTC value. I think I need my locale date instead?

在此处输入图片说明

I've tried this myself:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:( NSYearCalendarUnit |
                                                          NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:[NSDate date]];
[dateComponents setDay:1];
NSLog(@"%@", [gregorian dateFromComponents:dateComponents]);

And it properly prints 2015-04-30 22:00:00 +0000 which would be 1st of May after adding 2 hour (in my case) timezone difference. Just re-check your code, seems like self.calender.currentDate gives wrong month?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM