Subtract 7 days from current date

前端 未结 11 914
灰色年华
灰色年华 2021-01-29 22:41

It seems that I can\'t subtract 7 days from the current date. This is how i am doing it:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:N         


        
相关标签:
11条回答
  • 2021-01-29 23:14

    Swift 4.2 - Mutate (Update) Self

    Here is another way the original poster can get one week ago if he already has a date variable (updates/mutates itself).

    extension Date {
        mutating func changeDays(by days: Int) {
            self = Calendar.current.date(byAdding: .day, value: days, to: self)!
        }
    }
    

    Usage

    var myDate = Date()       // Jan 08, 2019
    myDate.changeDays(by: 7)  // Jan 15, 2019
    myDate.changeDays(by: 7)  // Jan 22, 2019
    myDate.changeDays(by: -1) // Jan 21, 2019
    

    or

    // Iterate through one week
    for i in 1...7 {
        myDate.changeDays(by: i)
        // Do something
    }
    
    0 讨论(0)
  • 2021-01-29 23:15

    use dateByAddingTimeInterval method:

    NSDate *now = [NSDate date];
    NSDate *sevenDaysAgo = [now dateByAddingTimeInterval:-7*24*60*60];
    NSLog(@"7 days ago: %@", sevenDaysAgo);
    

    output:

    7 days ago: 2012-04-11 11:35:38 +0000
    

    Hope it helps

    0 讨论(0)
  • 2021-01-29 23:22

    Swift 3.0+ version of the original accepted answer

    Date().addingTimeInterval(-7 * 24 * 60 * 60)

    However, this uses absolute values only. Use calendar answers is probably more suitable in most cases.

    0 讨论(0)
  • 2021-01-29 23:27

    code:

    NSDate *currentDate = [NSDate date];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setDay:-7];
    NSDate *sevenDaysAgo = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:currentDate options:0];
    NSLog(@"\ncurrentDate: %@\nseven days ago: %@", currentDate, sevenDaysAgo);
    [dateComponents release];
    

    output:

    currentDate: 2012-04-22 12:53:45 +0000
    seven days ago: 2012-04-15 12:53:45 +0000
    

    And I'm fully agree with JeremyP.

    BR.
    Eugene

    0 讨论(0)
  • 2021-01-29 23:27

    dymv's answer work great. This you can use in swift

    extension NSDate {    
        static func changeDaysBy(days : Int) -> NSDate {
            let currentDate = NSDate()
            let dateComponents = NSDateComponents()
            dateComponents.day = days
            return NSCalendar.currentCalendar().dateByAddingComponents(dateComponents, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!
        }
    }
    

    You can call this with

    NSDate.changeDaysBy(-7) // Date week earlier
    NSDate.changeDaysBy(14) // Date in next two weeks
    

    Hope it helps and thx to dymv

    0 讨论(0)
  • 2021-01-29 23:27

    Swift 3:

    A modification to Dov's answer.

    extension Date {
    
        func dateBeforeOrAfterFromToday(numberOfDays :Int?) -> Date {
    
            let resultDate = Calendar.current.date(byAdding: .day, value: numberOfDays!, to: Date())!
            return resultDate
        }
    }
    

    Usage:

    let dateBefore =  Date().dateBeforeOrAfterFromToday(numberOfDays : -7)
    let dateAfter = Date().dateBeforeOrAfterFromToday(numberOfDays : 7)
    print ("dateBefore : \(dateBefore), dateAfter :\(dateAfter)")
    
    0 讨论(0)
提交回复
热议问题