基本上,就像标题说的那样。我想知道如何添加1天的NSDate。
如果它是:
21st February 2011
它将变成:
22nd February 2011
或者如果它是:
31st December 2011
它将变成:
1st January 2012.
基本上,就像标题说的那样。我想知道如何添加1天的NSDate。
如果它是:
21st February 2011
它将变成:
22nd February 2011
或者如果它是:
31st December 2011
它将变成:
1st January 2012.
当前回答
只是为了好玩,通过一些扩展和操作符重载,你可以得到一些不错的东西,比如:
let today = Date()
let tomorrow = today + 1.days
, or
var date = Date()
date += 1.months
下面是支持代码:
extension Calendar {
struct ComponentWithValue {
let component: Component
let value: Int
}
}
extension Int {
var days: Calendar.ComponentWithValue {
.init(component: .day, value: self)
}
var months: Calendar.ComponentWithValue {
.init(component: .month, value: self)
}
}
func +(_ date: Date, _ amount: Calendar.ComponentWithValue) -> Date {
Calendar.current.date(byAdding: amount.component, value: amount.value, to: date)!
}
func +(_ amount: Calendar.ComponentWithValue, _ date: Date) -> Date {
date + amount
}
func +=(_ date: inout Date, _ amount: Calendar.ComponentWithValue) {
date = date + amount
}
代码是最少的,并且可以很容易地扩展到允许.月,.年,.小时等。还可以无缝添加对减法(-)的支持。
虽然在+操作符的实现中有一个强制的展开,但是不确定在哪种情况下日历可以返回nil日期。
其他回答
斯威夫特4.0
extension Date {
func add(_ unit: Calendar.Component, value: Int) -> Date? {
return Calendar.current.date(byAdding: unit, value: value, to: self)
}
}
使用
date.add(.day, 3)! // adds 3 days
date.add(.day, -14)! // subtracts 14 days
注意:如果你不知道为什么代码行以感叹号结尾,可以在谷歌上查找“Swift optional”。
基于highmaintenance的回答和vikingosegundo的评论,一个工作的Swift 3+实现。这个日期扩展也有额外的选项来改变年,月和时间:
extension Date {
/// Returns a Date with the specified amount of components added to the one it is called with
func add(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date? {
let components = DateComponents(year: years, month: months, day: days, hour: hours, minute: minutes, second: seconds)
return Calendar.current.date(byAdding: components, to: self)
}
/// Returns a Date with the specified amount of components subtracted from the one it is called with
func subtract(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date? {
return add(years: -years, months: -months, days: -days, hours: -hours, minutes: -minutes, seconds: -seconds)
}
}
如果按照OP的要求只增加一天,则用法如下:
let today = Date() // date is then today for this example
let tomorrow = today.add(days: 1)
NSDateComponents *dayComponent = [[[NSDateComponents alloc] init] autorelease];
dayComponent.day = 1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
dateToBeIncremented = [theCalendar dateByAddingComponents:dayComponent toDate:dateToBeIncremented options:0];
好吧,我以为这对我有用。但是,如果您使用它在2013年3月31日之后添加一天,它将返回一个只添加了23小时的日期。它实际上可能有24小时,但在计算中只添加了23:00小时。
类似地,如果您向前扩展到2013年10月28日,代码将增加25小时,从而导致日期时间为2013-10-28 01:00:00。
为了添加一天,我正在做的事情在顶部,添加:
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
复杂,主要是由于夏令时。
Swift 4更新:
let now = Date() // the current date/time
let oneDayFromNow = Calendar.current.date(byAdding: .day, value: 1, to: now) // Tomorrow with same time of day as now
只是为了好玩,通过一些扩展和操作符重载,你可以得到一些不错的东西,比如:
let today = Date()
let tomorrow = today + 1.days
, or
var date = Date()
date += 1.months
下面是支持代码:
extension Calendar {
struct ComponentWithValue {
let component: Component
let value: Int
}
}
extension Int {
var days: Calendar.ComponentWithValue {
.init(component: .day, value: self)
}
var months: Calendar.ComponentWithValue {
.init(component: .month, value: self)
}
}
func +(_ date: Date, _ amount: Calendar.ComponentWithValue) -> Date {
Calendar.current.date(byAdding: amount.component, value: amount.value, to: date)!
}
func +(_ amount: Calendar.ComponentWithValue, _ date: Date) -> Date {
date + amount
}
func +=(_ date: inout Date, _ amount: Calendar.ComponentWithValue) {
date = date + amount
}
代码是最少的,并且可以很容易地扩展到允许.月,.年,.小时等。还可以无缝添加对减法(-)的支持。
虽然在+操作符的实现中有一个强制的展开,但是不确定在哪种情况下日历可以返回nil日期。