RAGOpoR
RAGOpoR

Reputation: 8168

Need advice about time comparison using NSDate

I am developing Alarm Clock. I want to compare a time now and setTime. Is it possible to compare in minutes only.

My Problem is NSDate will compare in seconds, for example 9:38:50 is not equal to 9:38:00. how can I compare in minutes?

Upvotes: 1

Views: 978

Answers (2)

Chuck
Chuck

Reputation: 237110

Use NSCalendar to get the NSDateComponents you're interested in. Then it's easy to compare those.

Though it might be sufficient to check whether the current date is later than the alarm date. If it is and the alarm wasn't stopped, you should probably go off even if the computer momentarily lost power or something.

Upvotes: 2

iwat
iwat

Reputation: 3851

First, convert them to UNIX timestamp ignoring milliseconds.

NSUInteger time1 = (NSUInteger)[date1 timeIntervalSince1970];
NSUInteger time2 = (NSUInteger)[date2 timeIntervalSince1970];

Ignore seconds.

time1 = time1 - (time1 % 60);
time2 = time2 - (time2 % 60);

Now, you can safely compare them.

if (time1 == time2) {
  NSLog(@"Bingo");
}

Upvotes: 5

Related Questions