簡體   English   中英

當前星期的開始日期(如果星期總是從星期日開始)

[英]Date of the start of the current week (If weeks always start on Sunday)

我不確定這是否是最好的方法...但是...我需要獲取日期/時間,因為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM