NSDate beginning of day and end of day

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


        
23条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 00:08

    I think the most succinct way to do this in Swift is as follows:

    extension Date {
        func startOfDay() -> Date {
            return Calendar.current.startOfDay(for: self)
        }
        func endOfDay() -> Date {
            return Calendar.current.date(bySettingHour: 23, minute: 59, second: 59, of: self) ?? self
        }
    }
    

提交回复
热议问题