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 に答える