NSDate beginning of day and end of day

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


        
23条回答
  •  执笔经年
    2020-11-30 00:17

    This is what I use for Swift 4.2:

        let calendar = Calendar.current
        let fromDate = calendar.startOfDay(for: Date())
        let endDate = calendar.date(bySettingHour: 23, minute: 59, second: 59, of: Date())
    

    Works like a charm for me. You could add this to an extension for start and end dates on Date, however keep in mind that adding an extension increases compile time (unless in the same file as the class), so if you only need it at one place or in one class... don't use an extension.

提交回复
热议问题