NSDate beginning of day and end of day

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


        
23条回答
  •  暖寄归人
    2020-11-30 00:18

    Start Of Day / End Of Day — Swift 4

      // Extension
    
    extension Date {
        var startOfDay: Date {
            return Calendar.current.startOfDay(for: self)
        }
    
        var endOfDay: Date {
            var components = DateComponents()
            components.day = 1
            components.second = -1
            return Calendar.current.date(byAdding: components, to: startOfDay)!
        }
    
        var startOfMonth: Date {
            let components = Calendar.current.dateComponents([.year, .month], from: startOfDay)
            return Calendar.current.date(from: components)!
        }
    
        var endOfMonth: Date {
            var components = DateComponents()
            components.month = 1
            components.second = -1
            return Calendar.current.date(byAdding: components, to: startOfMonth)!
        }
    }
    
    // End of day = Start of tomorrow minus 1 second
    // End of month = Start of next month minus 1 second
    

提交回复
热议问题