NSDate beginning of day and end of day

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


        
相关标签:
23条回答
  • 2020-11-29 23:55

    Calendar units should be thought of as intervals. As of iOS 10 Calendar has some nice methods for this

    let day = Calendar.autoupdatingCurrent.dateInterval(of: .day, for: Date())
    day?.end
    day?.start
    

    You can use the same method, to get the start/end of any calendar component (week/month/year etc)

    0 讨论(0)
  • 2020-11-29 23:56

    Swift 5.1 - XCode 11 with Date class instead of NSDate and Calender instead of NSCalender

    extension Date {
    
        var startOfDay : Date {
            let calendar = Calendar.current
            let unitFlags = Set<Calendar.Component>([.year, .month, .day])
            let components = calendar.dateComponents(unitFlags, from: self)
            return calendar.date(from: components)!
       }
    
        var endOfDay : Date {
            var components = DateComponents()
            components.day = 1
            let date = Calendar.current.date(byAdding: components, to: self.startOfDay)
            return (date?.addingTimeInterval(-1))!
        }
    }
    

    Usage:

        let myDate = Date()
        let startOfDate = myDate.startOfDay
        let endOfDate = myDate.endOfDay
    
    0 讨论(0)
  • 2020-11-29 23:57

    Swift 5 Simple and more precise answer.

    Start time: 00:00:00

    End time: 23:59:59.5

    let date = Date() // current date or replace with a specific date
    let calendar = Calendar.current
    let startTime = calendar.startOfDay(for: date)
    let endTime = calendar.date(bySettingHour: 23, minute: 59, second: 59, of: date)
    

    Extra

    let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: noon)!
    let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: noon)!
    let specificDate = Date("2020-01-01")
    
    extension Date {
        init(_ dateString:String) {
            let dateStringFormatter = DateFormatter()
            dateStringFormatter.dateFormat = "yyyy-MM-dd"
            dateStringFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale
            let date = dateStringFormatter.date(from: dateString)!
            self.init(timeInterval:0, since:date)
        }
    }
    
    0 讨论(0)
  • 2020-11-29 23:58

    For swift 4

        var calendar = Calendar.current
        calendar.timeZone = NSTimeZone(abbreviation: "UTC")! as TimeZone
        let dateAtMidnight = calendar.startOfDay(for: Date())
    
        //For End Date
        var components = DateComponents()
        components.day = 1
        components.second = -1
    
        let dateAtEnd = calendar.date(byAdding: components, to: dateAtMidnight)
    
        print("dateAtMidnight :: \(dateAtMidnight)")
        print("dateAtEnd :: \(dateAtEnd!)")
    
    0 讨论(0)
  • 2020-11-29 23:59

    You are missing NSDayCalendarUnit in

    NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];
    
    0 讨论(0)
  • 2020-11-30 00:00

    My Swift extensions for NSDate:

    Swift 1.2

    extension NSDate {
    
        func beginningOfDay() -> NSDate {
            var calendar = NSCalendar.currentCalendar()
            var components = calendar.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay, fromDate: self)
            return calendar.dateFromComponents(components)!
        }
    
        func endOfDay() -> NSDate {
            var components = NSDateComponents()
            components.day = 1
            var date = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: self.beginningOfDay(), options: .allZeros)!
            date = date.dateByAddingTimeInterval(-1)!
            return date
        }
    }
    

    Swift 2.0

    extension NSDate {
    
        func beginningOfDay() -> NSDate {
            let calendar = NSCalendar.currentCalendar()
            let components = calendar.components([.Year, .Month, .Day], fromDate: self)
            return calendar.dateFromComponents(components)!
        }
    
        func endOfDay() -> NSDate {
            let components = NSDateComponents()
            components.day = 1
            var date = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: self.beginningOfDay(), options: [])!
            date = date.dateByAddingTimeInterval(-1)
            return date
        }
    }
    
    0 讨论(0)
提交回复
热议问题