如何在iPhone sdk中获取当前日期的下一个和更早的日期 [英] How do I get the next and earlier date of the current date in iPhone sdk

查看:101
本文介绍了如何在iPhone sdk中获取当前日期的下一个和更早的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个约会对象,

NSDate * date = [NSDate date];

NSDate *date = [NSDate date];

现在我想要在此日期之前和之后的日期。我应该使用什么功能。

Now I want a date earlier of this date and next of it. What function should I use.

早期和晚期是指前一天和后一天。

Earlier and Later means a day before and a day after.

我也是尝试使用earlyDate,但可能是我使用它错了,因为它返回相同的日期。

Also I tried using earlierDate but may be I am using it wrong since it is returning the same date.

tnx。

推荐答案

NSDate有点用词不当。这不是一个简单的日期,它是一个时间日期,具有亚秒级精度。 earlyDate:方法是一个比较函数,它返回两个日期中较早的日期,它不会创建较早的日期。如果您关心的是您当前所关注的日期,则可以执行以下操作:

NSDate is a bit of a misnomer. It is a not a simple date, it is a date a time, with subsecond level precision. The earlierDate: method is a comparison function that returns which ever of the two dates is earlier, it does not create an earlier date. If all you care about is a date that is before your current one you can do:

NSDate *earlierDate = [date addTimeInterval:-1.0];

这将在日期前1秒返回日期。同样你可以做到

Which will return a date 1 second before date. Likewise you can do

NSDate *laterDate = [date addTimeInterval:1.0];

未来1秒的日期。

如果您想要提前一天,可以添加几天的秒数。当然这是近似的,除非你处理所有格里历日历问题,但如果你只想快速近似:

If you want a day earlier you can add a days worth of seconds. Of course that is approximate unless you deal with all the gregorian calendar issues, but if you just want a quick approximation:

NSDate *earlierDate = [date addTimeInterval:(-1.0*24*60*60)];

这不适用于闰秒等,但对于大多数用途来说它可能没问题。

That doesn't work with leap seconds, etc, but for most uses it is probably fine.

这篇关于如何在iPhone sdk中获取当前日期的下一个和更早的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆