Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check to see if NSDate was 6 months ago

I've returned a NSDate value back from my server and I need to know how long ago this NSDate was. I need to write a conditional to see if the date is older than 6 months (in Swift, but I can convert Objective-C).

Here is the NSDate returned, when printed in console:

 (2015-09-07 01:22:01 +0000)

How do I check if this date above is older than 6 months?

The logic would be something like this:

//dateValue is the NSDate returned
var dateValue = log.createdAt
    if (dateValue > 6 months){
    }
    else{
    }
like image 795
Josh O'Connor Avatar asked Oct 23 '25 18:10

Josh O'Connor


1 Answers

Take a look at the NSCalendar class, and specifically methods like components:fromDate:toDate:options:.

If the 2 dates are different by 6 months and one day, is that > 6 months? How about 6 months and 1 second? Or do you only consider the 2 dates to be different by > 6 months if they differ by at least 7 months?

You would need to code your solution differently depending on the exact result you're after.

like image 184
Duncan C Avatar answered Oct 25 '25 08:10

Duncan C