3

直近の日曜日の午前12:00の日付オブジェクトを動的に作成する方法を理解するのに苦労しています。

今日の日付を取得してから、現在の曜日+ 1を引くことができると思っていました。その時点で、時刻を差し引くだけで午前12時になります。

これまでのところ、現在の曜日があります。

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekday = [comps weekday];

その時点で、今日の日付を取得し、1日のweekday*秒の差を引くことができます。しかし、どうすれば今日の時刻を秒単位で取得できますか?

4

4 に答える 4

7

秒を手動で計算する必要はありません(夏時間などのためにとにかく危険です)。以下はあなたが望むことを正確に行うはずです:

NSDate *today = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-US"]]; // force US locale, because other countries (e.g. the rest of the world) might use different weekday numbering

NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];

[nowComponents setWeekday:1]; //Sunday
[nowComponents setHour:0]; // 12:00 AM = midnight (12:00 PM would be 12)
[nowComponents setMinute:0];
[nowComponents setSecond:0];

NSDate *previousSunday = [calendar dateFromComponents:nowComponents];
于 2012-07-19T16:34:37.340 に答える
2

夏時間の間の移行について心配することはお任せします。

NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *date = [NSDate date];
NSLog(@"date %@", date);

NSDateComponents *componentsToday = [gregorian components:NSWeekdayCalendarUnit fromDate:date]; // NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | 
NSInteger days = componentsToday.weekday - 1;
NSLog(@"Days=%d", days);

NSDate *lastSunday = [date dateByAddingTimeInterval:-days*60*60*24];
NSLog(@"lastSunday %@", lastSunday);

NSDateComponents *componentsSunday = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit  fromDate:lastSunday];
[componentsSunday setHour:0];
[componentsSunday setMinute:0];
[componentsSunday setSecond:0];
NSDate *targetDate = [gregorian dateFromComponents:componentsSunday];
NSLog(@"TargetDate %@", targetDate);
于 2012-07-19T17:02:19.203 に答える
2

Swift 2.0の@AndreasLeyソリューション:

  func getLastSunday() -> NSDate?
{
    let today = NSDate();
    let calendar : NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
    calendar.locale = NSLocale(localeIdentifier: "en-US") // force US locale, because other countries (e.g. the rest of the world) might use different weekday numbering

    let flags : NSCalendarUnit = [.Year , .Month, .Weekday, .WeekOfMonth, .Minute , .Second]
    let nowComponents = calendar.components(flags, fromDate: today)

    nowComponents.weekday = 1 //Sunday
    nowComponents.hour = 0  // 12:00 AM = midnight (12:00 PM would be 12)
    nowComponents.minute = 0
    nowComponents.second = 0;


    let  previousSunday = calendar.dateFromComponents(nowComponents);
    return previousSunday
}
于 2016-02-24T10:07:01.103 に答える
0

このコードは、UIDatePickerから選択した日付の午前12:00を設定しています。

 NSDate *oldDateSelected = datePicker.date;
 unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:unitFlags fromDate:oldDateSelected];
    comps.hour   = 00;
    comps.minute = 00;
    comps.second = 44;
    NSDate *newDate = [calendar dateFromComponents:comps];
    return newDate;
于 2014-09-26T06:14:55.257 に答える