0

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

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]; 

除了“本周开始(星期日)”之外,它们似乎都在工作。

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

感谢大家的帮助。

4

1 回答 1

0

这可能不是最好的方法,但我得到了本周的第一天:

NSCalendar* cal = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents* comps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit fromDate:[NSDate date]];
NSDate* date = [cal dateFromComponents:comps];
于 2010-06-08T22:22:48.777 回答