如何向 NSDate 添加 1 天?
基本上就如标题所说。我想知道如何向 NSDate
添加 1 天。
因此,如果是:
21st February 2011
它将变成:
22nd February 2011
或者如果是:
31st December 2011
它将变成:
1st January 2012.
Basically, as the title says. I'm wondering how I could add 1 day to an NSDate
.
So if it were:
21st February 2011
It would become:
22nd February 2011
Or if it were:
31st December 2011
It would become:
1st January 2012.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
Swift 5.0:
目标 C:
这应该是不言自明的。
Swift 5.0 :
Objective C :
This should be self-explanatory.
从 iOS 8 开始,您可以使用 Swift 1.x 中的
NSCalendar.dateByAddingUnit
示例:
Swift 2.0:
Swift 3.0:
Since iOS 8 you can use
NSCalendar.dateByAddingUnit
Example in Swift 1.x:
Swift 2.0:
Swift 3.0:
Swift 5
Objective-C
Swift 5
Objective-C
基于 highmaintenance 答案和 vikingosegundo 的评论。此日期扩展还具有更改年、月和时间的附加选项:
按照 OP 的要求仅添加一天的用法将是:
A working Swift 3+ implementation based on highmaintenance's answer and vikingosegundo's comment. This Date extension also has additional options to change year, month and time:
Usage for only adding a day as asked by OP would then be:
iOS 8+、OSX 10.9+、Objective-C
iOS 8+, OSX 10.9+, Objective-C
Swift 4.0(与 这个精彩答案中的 Swift 3.0 相同,只是让像我这样的菜鸟明白)
Swift 4.0 (same as Swift 3.0 in this wonderful answer just making it clear for rookies like me)
Swift 4 更新:
Update for Swift 4:
使用下面的函数并使用 days 参数来获取日期 daysAhead/daysBehind 只需将参数传递为未来日期的正数或先前日期的负数:
Use the below function and use days paramater to get the date daysAhead/daysBehind just pass parameter as positive for future date or negative for previous dates:
迅速
In swift
有用!
It works!
Swift 3.0 非常简单的实现是:
Swift 3.0 very simple implementation would be:
Swift 4.0
用法
注意:如果您不知道为什么代码行以感叹号结尾,请在 Google 上查找“Swift Options”。
Swift 4.0
Usage
Note: If you don't know why the lines of code end with an exclamation point, look up "Swift Optionals" on Google.
昨天和明天的任何日期的简单 Swift 扩展:
我根据此处问题中的建议强制展开选项:
dateByAddingComponents:toDate:options 什么时候返回 nil?
Simple Swift extensions for yesterday and tomorrow from any date:
I'm force unwrapping the optionals based on advice in the question here:
When does dateByAddingComponents:toDate:options return nil?
向前和向后添加任意天数
在 Swift 2.1.1 和 xcode 7.1 OSX 10.10.5 中,您可以使用函数函数调用 将当前日期递增 9 天
函数调用 将当前日期递减 80 天
In Swift 2.1.1 and xcode 7.1 OSX 10.10.5 ,you can add any number of days forward and backwards using function
function call for incrementing current date by 9 days
function call for decrement current date by 80 days
这是一个通用方法,可让您在指定日期中添加/减去任何类型的单位(年/月/日/小时/秒等)。
使用Swift 2.2
Here is a general purpose method which lets you add/subtract any type of unit(Year/Month/Day/Hour/Second etc) in the specified date.
Using Swift 2.2
好的 - 我认为这对我有用。但是,如果您使用它向 2013 年 3 月 31 日添加一天,它将返回一个仅添加了 23 小时的日期。实际上很可能有 24,但在计算中仅添加了 23:00 小时。
同样,如果您向前移动到 2013 年 10 月 28 日,则代码会添加 25 小时,导致日期时间为 2013-10-28 01:00:00。
为了添加一天,我在顶部做了事情,添加:
复杂,主要是由于夏令时。
Ok - I thought this was going to work for me. However, if you use it to add a day to the 31st March 2013, it'll return a date that has only 23 hours added to it. It may well actually have the 24, but using in calculations has only 23:00 hours added.
Similarly, if you blast forward to 28th Oct 2013, the code adds 25 hours resulting in a date time of 2013-10-28 01:00:00.
In order to add a day I was doing the thing at the top, adding the:
Complicated, principally due to daylight saving.
您可以使用 NSDate 的方法
- (id)dateByAddingTimeInterval:(NSTimeInterval)seconds
其中seconds
将为60 * 60 * 24 = 86400
You can use NSDate's method
- (id)dateByAddingTimeInterval:(NSTimeInterval)seconds
whereseconds
would be60 * 60 * 24 = 86400
您可以快速扩展以在 NSDate 中添加方法,
您可以将其用作
In swift you can make extension to add method in NSDate
you can use this as
对于 swift 2.2:
希望这对某人有帮助!
for swift 2.2:
Hope this helps someone!
快速 5 的更新
update for swift 5
我也遇到了同样的问题;使用 NSDate 的扩展:
I had the same problem; use an extension for NSDate:
斯威夫特2.0
Swift 2.0
Swift 4,如果您真正需要的是 24 小时轮班(60*60*24 秒)而不是“1 个日历日”
未来:
let dayAhead = Date(timeIntervalSinceNow: TimeInterval(86400.0))
过去:
让 dayAgo = Date(timeIntervalSinceNow: TimeInterval(-86400.0))
Swift 4, if all you really need is a 24 hour shift (60*60*24 seconds) and not "1 calendar day"
Future:
let dayAhead = Date(timeIntervalSinceNow: TimeInterval(86400.0))
Past:
let dayAgo = Date(timeIntervalSinceNow: TimeInterval(-86400.0))
字符串扩展:转换String_Date>日期
日期扩展:转换日期>字符串
日期扩展:获取+/-日期
用法:
参考:来自多个问题
如何向 NSDate 添加 1 天?< /a>
数学函数将正整数转换为负数,将负数转换为正数?
将 NSString 转换为 NSDate(然后再转换回来)
String extension: Convert String_Date > Date
Date extension: Convert Date > String
Date extension: Get +/- Date
Usage:
reference: from multiple question
How do I add 1 day to an NSDate?
math function to convert positive int to negative and negative to positive?
Converting NSString to NSDate (and back again)
在 Swift 4 或 Swift 5 中,您可以使用如下所示:
输出:
In Swift 4 or Swift 5, you can use like bellow:
output:
只是为了好玩,通过一些扩展和运算符重载,您最终可以得到一些不错的东西,例如:
,或
下面是支持代码:
代码很少,并且可以轻松扩展以允许
.months
、.years
、.hours
等。还可以无缝添加对减法 (-
) 的支持。不过,在
+
运算符的实现中存在强制展开,但不确定在什么情况下日历可以返回零日期。Just for fun, with a few extensions and operator overloads, you can end up with something nice, like:
, or
Below is the support code:
The code is minimal, and can be easily extended to allow
.months
,.years
,.hours
, etc. Also support for subtraction (-
) can be seamlessly added.There is a forced unwrap, though, within the implementation of the
+
operator, however not sure under which circumstances can the calendar return a nil date.使用以下代码:
现在
已弃用。
Use following code:
As
is now deprecated.