ios 从当前日期减去 7 天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10209427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Subtract 7 days from current date
提问by Alex Tau
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.
SevenDaysAgo 获得与当前日期相同的值。
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.
编辑:在我的代码中,我忘记用正确的日期替换获取当前日期的变量。所以上面的代码是功能性的。
采纳答案by Novarg
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
希望能帮助到你
回答by dymv
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。
尤金
回答by Dov
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:
或者,使用 Swift 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 及更高版本,它变得更加紧凑:
let sevenDaysAgo = Calendar.current.date(byAdding: .day, value: -7, to: Date())
回答by Marckaraujo
Swift 3
斯威夫特 3
Calendar.current.date(byAdding: .day, value: -7, to: Date())
回答by Mark Moeykens
Swift 4.2 - Mutate (Update) Self
Swift 4.2 - 变异(更新)自我
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
}
回答by user3069232
Swift 4.2iOS 11.x Babec'ssolution, pure Swift
Swift 4.2iOS 11.x Babec 的解决方案,纯 Swift
extension Date {
static func changeDaysBy(days : Int) -> Date {
let currentDate = Date()
var dateComponents = DateComponents()
dateComponents.day = days
return Calendar.current.date(byAdding: dateComponents, to: currentDate)!
}
}
回答by Babac
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 有所帮助和感谢
回答by originalmyth
Swift 3.0+ version of the original accepted answer
原始接受答案的 Swift 3.0+ 版本
Date().addingTimeInterval(-7 * 24 * 60 * 60)
日期().addingTimeInterval(-7 * 24 * 60 * 60)
However, this uses absolute values only. Use calendar answers is probably more suitable in most cases.
但是,这仅使用绝对值。在大多数情况下,使用日历答案可能更合适。
回答by Sam
Using operator extension:
使用运算符扩展:
extension Date {
static func -(lhs: Date, rhs: Int) -> Date {
return Calendar.current.date(byAdding: .day, value: -rhs, to: lhs)!
}
}
Usage
用法
let today = Date()
let yesterday = today - 7
回答by A.G
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)")