2010-06-08 137 views
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]; 

它們似乎除 「當前周(週日)開始」 工作。

我該如何得到這個日期? (還是有更好的方法來做這一切?)

感謝大家的幫助。

回答

0

這可能不是最好的方式,但我得到當前一週的第一天這樣的:

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

這篇對你的工作? – JoeGaggler 2010-06-09 16:36:58

相關問題