从当前日期减去7天

时间:2022-08-25 18:47:33

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

我似乎不能从当前日期减去7天。我就是这样做的:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:-7];
NSDate *sevenDaysAgo = [gregorian dateByAddingComponents:offsetComponents toDate:[NSDate date] options:0];

SevenDaysAgo gets the same value as the current date.

seven days sago的值与当前日期相同。

Please help.

请帮助。

EDIT: In my code I forgot to replace the variable which gets the current date with the right one. So above code is functional.

编辑:在我的代码中,我忘记用正确的日期替换得到当前日期的变量。所以上面的代码是有用的。

8 个解决方案

#1


96  

use dateByAddingTimeInterval method:

使用dateByAddingTimeInterval方法:

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

希望它能帮助

#2


178  

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.

我完全同意JeremyP的观点。

BR.
Eugene

BR。尤金

#3


98  

If you're running at least iOS 8 or OS X 10.9, there's an even cleaner way:

如果你至少运行iOS 8或OS X 10.9,还有一种更干净的方式:

NSDate *sevenDaysAgo = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay
                                                                value:-7
                                                               toDate:[NSDate date]
                                                              options:0];

Or, with Swift 2:

或者,斯威夫特2:

let sevenDaysAgo = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -7,
    toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))

And with Swift 3 and up it gets even more compact:

通过Swift 3和up,它变得更加紧凑:

let sevenDaysAgo = Calendar.current.date(byAdding: .day, value: -7, to: Date())

#4


24  

Swift 3

Calendar.current.date(byAdding: .day, value: -7, to: Date())

#5


4  

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

dymv回答工作好了。这可以在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

希望它能对dymv有所帮助

#6


-1  

FOR SWIFT 3.0

斯威夫特3.0

here is fucntion , you can reduce days , month ,day by any count like for example here , i have reduced the current system date's year by 100 year , you can do it for day , month also just set the counter and store it in an array , you can this array anywhere then func currentTime()

因数,可以减少天,月,日的计数比如这里,我减少了当前系统日期的年到100年一年,你能做到一天,月也只是设置计数器并将它存储在一个数组,你可以在任何地方这个数组然后func currentTime()

 {

    let date = Date()
    let calendar = Calendar.current
    var year = calendar.component(.year, from: date)
    let month = calendar.component(.month, from: date)
    let  day = calendar.component(.day, from: date)
    let pastyear = year - 100
    var someInts = [Int]()
    printLog(msg: "\(day):\(month):\(year)" )

    for _ in pastyear...year        {
        year -= 1
                     print("\(year) ")
        someInts.append(year)
    }
          print(someInts)
        }

#7


-1  

Swift 3.0

斯威夫特3.0

let date = Date() - 7 * 24 * 60 * 60

#8


-1  

Swift 3:

斯威夫特3:

A modification to Dov's answer.

对Dov答案的修改。

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)")

#1


96  

use dateByAddingTimeInterval method:

使用dateByAddingTimeInterval方法:

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

希望它能帮助

#2


178  

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.

我完全同意JeremyP的观点。

BR.
Eugene

BR。尤金

#3


98  

If you're running at least iOS 8 or OS X 10.9, there's an even cleaner way:

如果你至少运行iOS 8或OS X 10.9,还有一种更干净的方式:

NSDate *sevenDaysAgo = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay
                                                                value:-7
                                                               toDate:[NSDate date]
                                                              options:0];

Or, with Swift 2:

或者,斯威夫特2:

let sevenDaysAgo = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -7,
    toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))

And with Swift 3 and up it gets even more compact:

通过Swift 3和up,它变得更加紧凑:

let sevenDaysAgo = Calendar.current.date(byAdding: .day, value: -7, to: Date())

#4


24  

Swift 3

Calendar.current.date(byAdding: .day, value: -7, to: Date())

#5


4  

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

dymv回答工作好了。这可以在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

希望它能对dymv有所帮助

#6


-1  

FOR SWIFT 3.0

斯威夫特3.0

here is fucntion , you can reduce days , month ,day by any count like for example here , i have reduced the current system date's year by 100 year , you can do it for day , month also just set the counter and store it in an array , you can this array anywhere then func currentTime()

因数,可以减少天,月,日的计数比如这里,我减少了当前系统日期的年到100年一年,你能做到一天,月也只是设置计数器并将它存储在一个数组,你可以在任何地方这个数组然后func currentTime()

 {

    let date = Date()
    let calendar = Calendar.current
    var year = calendar.component(.year, from: date)
    let month = calendar.component(.month, from: date)
    let  day = calendar.component(.day, from: date)
    let pastyear = year - 100
    var someInts = [Int]()
    printLog(msg: "\(day):\(month):\(year)" )

    for _ in pastyear...year        {
        year -= 1
                     print("\(year) ")
        someInts.append(year)
    }
          print(someInts)
        }

#7


-1  

Swift 3.0

斯威夫特3.0

let date = Date() - 7 * 24 * 60 * 60

#8


-1  

Swift 3:

斯威夫特3:

A modification to Dov's answer.

对Dov答案的修改。

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)")