Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.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
Iphone 当前周的开始日期(如果周始终从星期日开始)_Iphone_Macos_Nsdate_Nsdateformatter - Fatal编程技术网

Iphone 当前周的开始日期(如果周始终从星期日开始)

Iphone 当前周的开始日期(如果周始终从星期日开始),iphone,macos,nsdate,nsdateformatter,Iphone,Macos,Nsdate,Nsdateformatter,我不确定这是不是最好的方法。。。但是我需要获取日期/时间,因为: Midnight at the start of today Midnight at the start of the current week (Sunday) Midnight at the start of the current month Midnight at the start of the current year 我使用NSDateFormatter: [df setDateFormat:@"yyyy-MM-d

我不确定这是不是最好的方法。。。但是我需要获取日期/时间,因为:

Midnight at the start of today
Midnight at the start of the current week (Sunday)
Midnight at the start of the current month
Midnight at the start of the current year
我使用NSDateFormatter:

[df setDateFormat:@"yyyy-MM-dd 00:00:00"]; strDate = [df stringFromDate:today]; 
[df setDateFormat:@"yyyy-MM-dd 00:00:00"]; strDate = [df stringFromDate:today];  // Sunday??? 
[df setDateFormat:@"yyyy-MM-01 00:00:00"]; strDate = [df stringFromDate:today]; 
[df setDateFormat:@"yyyy-01-01 00:00:00"]; strDate = [df stringFromDate:today]; 
除了“本周(星期日)开始”之外,他们似乎都在工作

我怎样才能得到那个日期?(或者有没有更好的方法来实现这一切?)


谢谢大家的帮助。

这可能不是最好的方式,但我在本周的第一天就有了以下内容:

NSCalendar* cal = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents* comps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit fromDate:[NSDate date]];
NSDate* date = [cal dateFromComponents:comps];