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{
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With