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!
-
Answer:
-components:fromDate:toDate:options: method in NSCalendar class does exactly what you need - you can specify what date components you need in your difference setting corresponding flags in the 1st parameter (check reference docs for more details)
Stefano Mendicino at Stack Overflow Visit the source
Other answers
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.
Ishu
Related Q & A:
- How to search events between two dates in WP?Best solution by WordPress
- How do I compare two tables with the same column definitions in different schemas?Best solution by Database Administrators
- How to compare dates in Angularjs?Best solution by Stack Overflow
- Compare two columns in excel?Best solution by Super User
- Will 1 moving violation that happened two years ago cause me not to get this job?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.