Current Week Start and End Date

后端 未结 16 1363
轮回少年
轮回少年 2020-11-30 01:56

I want to get the current week start and end date and I also want to use the previous week start and end date and next week of the start and end date in current month.

16条回答
  •  旧巷少年郎
    2020-11-30 02:17

    In swift 3.0

    let cal = NSCalendar.current
    
    
    //weekday
    let weekday = cal.component(.weekday, from: Date())
    
    var dateComp = cal.dateComponents([.hour, .minute, .second, .day, .month, .year], from: Date())
    print(dateComp.day!)
    
    
    //Start Date of the week - Sunday
    dateComp.day = dateComp.day! - (weekday - 1)// start date of week
    
    print(cal.date(from: dateComp)!)
    
    
    //End Date of the Week - Saturday
    dateComp = cal.dateComponents([.hour, .minute, .second, .day, .month, .year], from: Date())
    
    
    dateComp.day = dateComp.day! + (7 - weekday)
    
    print(cal.date(from: dateComp)!)
    

提交回复
热议问题