从当前日期减去7天

似乎我不能从现在的日期中减去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 获取与当前日期相同的值。

请帮帮我。

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

80434 次浏览

使用 dateByAddingTimeInterval 方法:

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

产出:

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

希望能有帮助

密码:

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];

产出:

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

我完全同意 JeremyP。

BR.
尤金

如果你至少在运行 iOS 8或者 OS X 10.9,那么还有一个更干净的方法:

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

或者,用 Swift 2:

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

随着 Swift 3的出现,它变得更加紧凑:

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

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))!
}
}

你可以用

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

希望对你有帮助,谢谢 Dymv

Swift 3

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

为了 SWIFT 3.0

这里是功能,你可以减少天,月,天的任何计数 比如这里,我已经把当前系统日期的年份减少了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)
}

斯威夫特3:

对 Dov 回答的修改。

extension Date {


func dateBeforeOrAfterFromToday(numberOfDays :Int?) -> Date {


let resultDate = Calendar.current.date(byAdding: .day, value: numberOfDays!, to: Date())!
return resultDate
}
}

用法:

let dateBefore =  Date().dateBeforeOrAfterFromToday(numberOfDays : -7)
let dateAfter = Date().dateBeforeOrAfterFromToday(numberOfDays : 7)
print ("dateBefore : \(dateBefore), dateAfter :\(dateAfter)")

Swift 3.0 + 版本的原始接受答案

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

但是,这只使用绝对值。在大多数情况下,使用日历答案可能更合适。

Swift 4.2 iOS 11.x 巴贝克的解决方案,纯 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)!
}
}

Swift 4.2-突变(更新)自我

如果原始海报已经有一个日期变量(更新/变异本身) ,那么这里还有另一种方法可以让他在一周前得到。

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

用法

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

或者

// Iterate through one week
for i in 1...7 {
myDate.changeDays(by: i)
// Do something
}

快速接线员分机:

extension Date {
    

static func -(lhs: Date, rhs: Int) -> Date {
return Calendar.current.date(byAdding: .day, value: -rhs, to: lhs)!
}
}

用法

let today = Date()
let sevenDayAgo = today - 7

Swift 5

函数在当前日期中添加或减去日期。

func addOrSubtructDay(day:Int)->Date{
return Calendar.current.date(byAdding: .day, value: day, to: Date())!
}

现在调用函数

var dayAddedDate = addOrSubtructDay(7)
var daySubtractedDate = addOrSubtructDay(-7)
  • 添加日期传递预期日值
  • 减去负日 价值