dynamically create date for previous sunday at 12:00 AM

前端 未结 4 962
鱼传尺愫
鱼传尺愫 2021-01-14 16:13

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 subt

相关标签:
4条回答
  • 2021-01-14 16:21

    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);
    
    0 讨论(0)
  • 2021-01-14 16:22

    @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
    }
    
    0 讨论(0)
  • 2021-01-14 16:27

    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;
    
    0 讨论(0)
  • 2021-01-14 16:28

    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];
    
    0 讨论(0)
提交回复
热议问题