两个 NSDate 对象之间的差异 -- 结果也是一个 NSDate

发布于 2024-10-30 19:43:27 字数 213 浏览 5 评论 0原文

我有两个 NSDate 对象,我想要两者之间的差异,结果应该再次是一个 NSDate 对象。知道如何实现这一目标吗?

在这里,我试图解决一个独特的问题,我必须找出经过的时间,然后定位经过的时间。如果我有 NSDate 对象中的经过时间,我可以对其进行本地化。因此想到创建一个 NSDate 对象,其时间分量与两个日期之间的时间间隔相同,以便我可以使用 NSDateFormatter 来本地化它。

I have two NSDate objects and I want the difference between the two and the result should again be a NSDate object. Any idea how to achieve this?

Here, I am trying to address a unique problem where I have to find out the elapsed time and then localize the elapsed time. I can localize it if I have the elapsed time in NSDate object. So thought of creating a NSDate object which has its time component same as time interval between the two dates so that I could use NSDateFormatter to localize it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

向日葵 2024-11-06 19:43:27

NSDate 表示时间中的一个实例,因此将时间间隔表示为 NSDate 是没有意义的。你想要的是NSDateComponents

NSDate *dateA;
NSDate *dateB;

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay
                                           fromDate:dateA
                                             toDate:dateB
                                            options:0];

NSLog(@"Difference in date components: %i/%i/%i", components.day, components.month, components.year);

NSDate represents an instance in time, so it doesn't make sense to represent an interval of time as an NSDate. What you want is NSDateComponents:

NSDate *dateA;
NSDate *dateB;

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay
                                           fromDate:dateA
                                             toDate:dateB
                                            options:0];

NSLog(@"Difference in date components: %i/%i/%i", components.day, components.month, components.year);
甜尕妞 2024-11-06 19:43:27

如果从 2002 年 5 月 5 日减去 2001 年 12 月 12 日,日期是多少?两个日期之间的时间距离不能是日期,它始终是某种间隔。您可以使用 timeIntervalSinceDate: 计算间隔。

要本地化,您可以尝试以下步骤:

If you subtract 12/12/2001 from 05/05/2002 what will be the date? The chronological distance between two dates can't be a date, it's alway some kind of interval. You can use timeIntervalSinceDate: to calculate the interval.

To localize you can try the following steps:

伴我心暖 2024-11-06 19:43:27

NSDate 类参考中,您有实例方法来执行这些操作 -

  1. 如何比较两个 NSDate 变量? Ans: isEqualToDate:
  2. 如何查找两个 NSDate 变量之间的差异? 答: timeIntervalSinceDate:

From NSDate class reference, you have instance methods to do these -

  1. How to compare two NSDate variables? Ans: isEqualToDate:
  2. How to find difference between two NSDate variables? Ans: timeIntervalSinceDate:
安静 2024-11-06 19:43:27

您可以使用 NSDatetimeIntervalSinceDate: 计算两个日期之间的时间间隔,但将时间间隔表示为日期没有任何意义。

You can calculate the time interval between two dates using NSDate's timeIntervalSinceDate:, but it doesn't make any sense for you to represent a time interval as a date.

糖粟与秋泊 2024-11-06 19:43:27

在 NSDate 中使用 -compare: 有一个简单的方法:

NSDate *dateA = [NSDate dateWithTimeIntervalSinceNow:100];
NSDate *dateB = [NSDate dateWithTimeIntervalSinceNow:200];
NSDate *myDate = [NSDate dateWithTimeIntervalSinceNow:150];
NSArray *dateArray = [NSArray arrayWithObjects:dateA, dateB, myDate, nil];
NSArray *sortedArray = [dateArray sortedArrayUsingSelector:@selector(compare:)];
if ([myDate isEqualToDate:[sortedArray objectAtIndex:1]])
    NSLog(@"myDatea between dateA and dateB");

There is a easy way by using -compare: in NSDate:

NSDate *dateA = [NSDate dateWithTimeIntervalSinceNow:100];
NSDate *dateB = [NSDate dateWithTimeIntervalSinceNow:200];
NSDate *myDate = [NSDate dateWithTimeIntervalSinceNow:150];
NSArray *dateArray = [NSArray arrayWithObjects:dateA, dateB, myDate, nil];
NSArray *sortedArray = [dateArray sortedArrayUsingSelector:@selector(compare:)];
if ([myDate isEqualToDate:[sortedArray objectAtIndex:1]])
    NSLog(@"myDatea between dateA and dateB");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文