dynamically create date for previous sunday at 12:00 AM

左心房为你撑大大i 提交于 2019-12-01 06:05:10

问题


I'm struggling to figure out how to dynamically create a date object for the most previous sunday at 12:00 AM

I was thinking I could get today's date and then subtract the current day of the week + 1 at which point I could just subtract the time of the day do get down to 12AM.

so far I have the current day of the week:

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

at which point I can get today's date and subtract the difference of weekday * seconds in a day. However, how can I get today's time in seconds ??


回答1:


No need to manually calculate seconds (which is dangerous anyway because of daylight saving etc.). The following should do exactly what you want:

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



回答2:


I'll leave worrying about transitions between daylight savings time to you:

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



回答3:


@AndreasLey solution in Swift 2.0:

  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
}



回答4:


This code is setting 12:00 am in your selected date from UIDatePicker.

 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;


来源:https://stackoverflow.com/questions/11565018/dynamically-create-date-for-previous-sunday-at-1200-am

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!