简体   繁体   English

如何获得我所在时区的实际第一个月第一天?

[英]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. 我正在尝试获取每月的第一天,但​​我正在获取UTC值。 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. 并正确打印2015-04-30 22:00:00 +0000 ,这将是5月1日加上2小时(在我的情况下)时区差异。 Just re-check your code, seems like self.calender.currentDate gives wrong month? 只是重新检查您的代码,好像self.calender.currentDate给错了月份?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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