NSDate beginning of day and end of day

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


        
相关标签:
23条回答
  • 2020-11-30 00:15

    Swift3 Using *XCode8
    Apple is removing the NS from the class name so that NSDate can be swapped out to Date. You may get a compiler warning if you try to cast them saying they will always fail, but they work fine when you run them in the playground.

    I replaced my generated NSDate in core data model with Date and they still work.

    extension Date {
      func startTime() -> Date {
        return Calendar.current.startOfDay(for: self)
      }
    
      func endTime() -> Date {
        var components = DateComponents()
        components.day = 1
        components.second = -1
        return Calendar.current.date(byAdding: components, to: startTime())!
      }
    }
    
    0 讨论(0)
  • 2020-11-30 00:17

    For me none of the answers here and else where on stackoverflow worked. To get start of today i did this.

    NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
    [gregorian setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];    
    NSDateComponents *components = [gregorian components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]]; 
    [components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
    NSDate *beginningOfToday = [gregorian dateFromComponents:components];
    

    Note this [gregorian setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; and [components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];.

    When a calendar is created it gets initialised with current timezone and when date is extracted from its components, since NSDate has no timezone, the date from current timezone is considered as UTC timezone. So we need to set the timezone before extracting components and later when extracting date from these components.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-30 00:19

    Objective-C

    NSCalendar * calendar = [NSCalendar currentCalendar];
    NSDate * startDate = [calendar startOfDayForDate:[NSDate date]];
    NSLog(@"start date is %@", startDate);
    
    0 讨论(0)
提交回复
热议问题