ios 两个 NSDate 对象之间的差异——结果也是一个 NSDate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5562594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between two NSDate objects -- Result also a NSDate
提问by Abhinav
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?
我有两个 NSDate 对象,我想要两者之间的差异,结果应该再次是一个 NSDate 对象。任何想法如何实现这一目标?
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.
在这里,我试图解决一个独特的问题,我必须找出经过的时间,然后定位经过的时间。如果我在 NSDate 对象中有经过的时间,我可以对它进行本地化。所以想创建一个 NSDate 对象,它的时间分量与两个日期之间的时间间隔相同,这样我就可以使用 NSDateFormatter 来本地化它。
回答by Nick Forge
NSDate
represents an instance in time, so it doesn't make sense to represent an intervalof time as an NSDate
. What you want is NSDateComponents
:
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);
回答by Nick Weaver
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.
如果你从 05/05/2002 减去 12/12/2001 是什么日期?两个日期之间的时间间隔不能是日期,它总是某种间隔。您可以使用timeIntervalSinceDate:来计算间隔。
To localize you can try the following steps:
要本地化,您可以尝试以下步骤:
You can use the NSCalendarwith dateFromComponents: passing in a NSDateComponents.
To break down a timeInterval into NSDateComponentslook at How do I break down an NSTimeInterval into year, months, days, hours, minutes and seconds on iPhone?.
Finally use the NSDateFormatterand initWithDateFormat:allowNaturalLanguage:to get your localized string. The Date Format String Syntaxshows the different placeholders.
您可以将NSCalendar与 dateFromComponents一起使用:传入NSDateComponents。
要将 timeInterval 分解为NSDateComponents,请查看如何在 iPhone 上将 NSTimeInterval 分解为年、月、日、小时、分钟和秒?.
最后使用NSDateFormatter和initWithDateFormat:allowNaturalLanguage:来获取本地化的字符串。该日期格式字符串语法显示不同的占位符。
回答by Jhaliya
From NSDate
class reference, you have instance methods to do these -
从NSDate
类参考,你有实例方法来做这些 -
- How to compare two NSDate variables? Ans:
isEqualToDate:
- How to find difference between two NSDate variables? Ans:
timeIntervalSinceDate:
- How to get each separate value of minute, hours and days from NSDate variable? links
- 如何比较两个 NSDate 变量?答:
isEqualToDate:
- 如何找到两个 NSDate 变量之间的差异?答:
timeIntervalSinceDate:
- 如何从 NSDate 变量中获取分钟、小时和天的每个单独值?链接
回答by Chris Doble
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.
您可以使用NSDate
's计算两个日期之间的时间间隔timeIntervalSinceDate:
,但将时间间隔表示为日期没有任何意义。
回答by neal
There is a easy way by using -compare: in NSDate:
在 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");