Adding a day to current date in iOS

前端 未结 6 1639
孤城傲影
孤城傲影 2021-01-02 11:03

I saw this post: iOS and finding Tomorrow.

The code provided is:

units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponen         


        
相关标签:
6条回答
  • 2021-01-02 11:43

    To add any number of any components to the date, for example: 2 days or 2 months, 1 year, do the following:

    Calendar.current.date(byAdding: DateComponents(month: -1), to: date)! //this create date with previous month for date.
    

    you can initialize DateComponents with anything:

    public init(calendar: Calendar? = default, timeZone: TimeZone? = default, era: Int? = default, year: Int? = default, month: Int? = default, day: Int? = default, hour: Int? = default, minute: Int? = default, second: Int? = default, nanosecond: Int? = default, weekday: Int? = default, weekdayOrdinal: Int? = default, quarter: Int? = default, weekOfMonth: Int? = default, weekOfYear: Int? = default, yearForWeekOfYear: Int? = default)
    
    0 讨论(0)
  • 2021-01-02 11:46

    For swift googlers:

    NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: 1, toDate: date, options: nil)!
    
    0 讨论(0)
  • 2021-01-02 11:52

    Form a date component with number of days you want to add to your original date. From the current calendar form the date by adding the this component to your original date.

    NSDateComponents *dateComponents = [NSDateComponents new];
    dateComponents.day = 1;
    NSDate *newDate = [[NSCalendar currentCalendar]dateByAddingComponents:dateComponents 
                                                                   toDate: selectedDate 
                                                                  options:0];
    
    0 讨论(0)
  • 2021-01-02 11:54

    Simple and straight forward solution :

    NSDate *currentDate = [NSDate date];
    
    NSDate *nextDate = [currentDate dateByAddingTimeInterval:(24*3600)]; 
    

    This is working fine for me.

    0 讨论(0)
  • 2021-01-02 12:02

    First Thing,you are assigning day instead of month to NSDateComponent. Execute following code and find difference between inDate And newDate

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitMonth fromDate:inDate];
    

    NSInteger monthFlag = 1; components.month = minuteFlag;

    // Retrieve date with increased days count
    NSDate *newDate = [[NSCalendar currentCalendar]
                       dateByAddingComponents:components
                                       toDate:monthFlag
                                      options:0];
    
    0 讨论(0)
  • 2021-01-02 12:03

    Here is a method I use:

    + (NSDate *)addDays:(NSInteger)days toDate:(NSDate *)originalDate {
        NSDateComponents *components= [[NSDateComponents alloc] init];
        [components setDay:days];
        NSCalendar *calendar = [NSCalendar currentCalendar];
        return [calendar dateByAddingComponents:components toDate:originalDate options:0];
    }
    

    With it, you can add as many days as you want. It also works with negative numbers, so you can subtract days.

    0 讨论(0)
提交回复
热议问题