我不确定这是否是最好的方法......但是......我需要得到日期/时间:
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];
除了“本周开始(星期日)”之外,它们似乎都有效。
我如何获得该日期? (或者有更好的方法来完成所有这些吗?)
感谢大家的帮助。
答案 0 :(得分:0)
这可能不是最好的方法,但是我得到了本周的第一天:
NSCalendar* cal = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents* comps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit fromDate:[NSDate date]];
NSDate* date = [cal dateFromComponents:comps];