Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if one NSDate is greater than another

if (datStartDate > datEndDate) {

This doesn't seem to work. I know there's a isEqual, etc., but how do I perform "is greater than"?

There are both NSDate.

like image 714
Jules Avatar asked Oct 01 '10 12:10

Jules


2 Answers

The easiest method i'm aware is:

if( [firstDate timeIntervalSinceDate:secondDate] > 0 ) {

The other answers cover compare:, wanted to add some flavour ;).

like image 113
dannywartnaby Avatar answered Nov 06 '22 19:11

dannywartnaby


To compare dates use -compare: method:

Return Value If:

  • The receiver and anotherDate are exactly equal to each other, NSOrderedSame
  • The receiver is later in time than anotherDate, NSOrderedDescending
  • The receiver is earlier in time than anotherDate, NSOrderedAscending.
like image 32
Vladimir Avatar answered Nov 06 '22 17:11

Vladimir