Compare two dates and get the difference in years, months, days
How can I know the number of years, months, weeks, days, hours and minutes from a date in the past (fixed) to now?
For example I want to compare: date1: 2009/11/10 8:23 date2: 2010/12/17 10:27
and obtain as a result: 1 year, 1 month, 1 week, 2 hours, 4 minutes
Many thanks!
-components:fromDate:toDate:options:
NSCalendar
类中的方法完全符合你的需求 - 你可以在差异中指定你需要的日期组件在第一个参数中设置相应的标志(更多详细信息请参考参考文档)
NSDate *today = [NSDate date];
NSTimeInterval interval = [today timeIntervalSinceDate:lastDate];
int weeksForLastDate=interval/604800;
this gives you weeks similerly you can find years etc. so you need to implement this logic according to you and also date format according to you.
here interval gives you time in seconds so convert it according to you.
链接地址: http://www.djcxy.com/p/36714.html上一篇: Date()对象不一致
下一篇: 比较两个日期并获得年,月,日的差异