Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSCalendar dateFromComponents返回错误的日期2_Ios_Calendar_Components_Nsdate_Nsdatecomponents - Fatal编程技术网

Ios NSCalendar dateFromComponents返回错误的日期2

Ios NSCalendar dateFromComponents返回错误的日期2,ios,calendar,components,nsdate,nsdatecomponents,Ios,Calendar,Components,Nsdate,Nsdatecomponents,我想知道一年中口腔第一周的日期: NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setYear:2013]; [components setMonth:1]; [components setWeekOfMonth:1]; [components setWeekday:1]; NSDate

我想知道一年中口腔第一周的日期:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:2013];
[components setMonth:1];
[components setWeekOfMonth:1];
[components setWeekday:1];
NSDate *newDate = [calendar dateFromComponents:components];
NSLog(@"%@",newDate);
我得到的是:

2012-12-29 23:00:00 +0000
与mac日历相比,我需要的是:

2012-12-31 23:00:00 +0000

有什么建议吗?

问题可能是设置
工作日
这是工作代码

 NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setYear:2013];
    [components setMonth:1];
    [components setDay:1];

    NSDate *newDate = [calendar dateFromComponents:components];
    NSLog(@"%@",newDate); //2012-12-31 23:00:00 +0000
您可以使用
nsgregoriacalendar
,而不是
CurrentCalendar

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
[comp setYear:2013];
[comp setMonth:1];
[comp setDay:1];
NSDate *firstDayOfMonthDate = [gregorian dateFromComponents:comp];
NSLog(@"%@",firstDayOfMonthDate);  // 2012-12-31 23:00:00 +0000
(我知道你现在已经知道发生了什么,但为了未来的读者……)

看看你在这里做什么:

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:2013];
[components setMonth:1];
[components setWeekOfMonth:1];
[components setWeekday:1];
让我们以今天(2013年2月28日)为例,看看我们在每一步之后得到了什么(假设;我无法检查这一点!):

  • setYear:2013
    -没有变化,因为这一年已经是2013年了
  • setMonth:1
    -更改为1月:2013-01-28
  • setWeekOfMonth:1
    -更改为2013年1月第一周的每周同一天(星期四):2013-01-03
  • 设置工作日:1
    -更改为同一周的星期日:2012-12-30
现在,当您打印2012-12-30的当地午夜,但在UTC中,您得到的是“2012-12-29 23:00:00+0000”,因为您的当地时区可能比UTC早1小时


既然你已经确定了,你想要的是
setDay
而不是
setWeekOfMonth
/
setWeekday
,假设你真的想要的是“一月一日”而不是“一月一日的星期日”。

好的,如果答案对你有帮助,你可以接受,其次,我编辑了输出的答案。问题不是使用NSGregorianCalendar而不是currentCalendar,特别是setDay vs setWeekday;您的代码在这方面有所更改,但您没有在文本中提到这一点。我建议你编辑一下,解释一下之前发生了什么。@NSG:好吧,但并不是说NSGregorianCalendar是一个替代品-你仍然需要使用
setDay
。我添加了一个答案,解释了我认为在原始代码中发生了什么。@JonSkeet date将显示预期结果,即使您没有设置day@nsgulliver:绝对-值得保留:)