如何在iPhone sdk中获取当前日期的下一个和更早的日期

时间:2022-04-19 07:31:19

I have a date object,

我有一个约会对象,

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.

早期和晚期意味着前一天和后一天。

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

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

tnx.

2 个解决方案

#1


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

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

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

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

NSDate *laterDate = [date addTimeInterval:1.0];

for a date 1 second in the future.

为将来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.

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

#2


You need to use NSCalendar and NSDateComponents to do this calculation reliably.

您需要使用NSCalendar和NSDateComponents可靠地执行此计算。

Here's an example I have to hand to compute an NSDate for noon today. I'm sure you can work out how to get the result you want from this and the documentation.

这是我今天要为中午计算NSDate的一个例子。我相信你可以找出如何从这个和文档中获得你想要的结果。

NSDate *today = [NSDate date];

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDateComponents* todayComponents = [gregorian components:unitFlags fromDate:today];

todayComponents.hour = 12;

NSDate* noonToday = [gregorian dateFromComponents:todayComponents];

#1


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

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

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

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

NSDate *laterDate = [date addTimeInterval:1.0];

for a date 1 second in the future.

为将来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.

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

#2


You need to use NSCalendar and NSDateComponents to do this calculation reliably.

您需要使用NSCalendar和NSDateComponents可靠地执行此计算。

Here's an example I have to hand to compute an NSDate for noon today. I'm sure you can work out how to get the result you want from this and the documentation.

这是我今天要为中午计算NSDate的一个例子。我相信你可以找出如何从这个和文档中获得你想要的结果。

NSDate *today = [NSDate date];

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDateComponents* todayComponents = [gregorian components:unitFlags fromDate:today];

todayComponents.hour = 12;

NSDate* noonToday = [gregorian dateFromComponents:todayComponents];