NSDate beginning of day and end of day

后端 未结 23 2105
广开言路
广开言路 2020-11-29 23:28
    -(NSDate *)beginningOfDay:(NSDate *)date
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:( NSMonthCalend         


        
23条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 00:00

    Just for reference, simple way to set Start and End of the day in Swift 4,

    var comp: DateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: Date())
    comp.hour = 0
    comp.minute = 0
    comp.second = 0
    comp.timeZone = TimeZone(abbreviation: "UTC")!
    
    
    //Set Start of Day
    let startDate : Date = Calendar.current.date(from: comp)!
    print(“Day of Start : \(startDate)")
    
    
    //Set End of Day
    comp.hour = 23
    comp.minute = 59
    comp.second = 59
    
    let endDate : Date = Calendar.current.date(from:comp)!
    print("Day of End : \(endDate)")
    

提交回复
热议问题