nickdnk
nickdnk

Reputation: 4300

Get current date past midnight, Objective C

I want to get the current date, even if the time has passed midnight. Imagine it's friday night the 6th of June 2014 - we check the date Saturday at 2 am, but we still want this to count as being friday. How would I go about this?

Let's just say we cut it at 9am the next day. I.e. we will assume previous date until the time has passed 9 am. Yes, this is software used at a nightclub, as you can imagine.

I guess this would involve something like subtracting 1 day from the current date if the hour is less than 10?

Upvotes: 0

Views: 263

Answers (2)

user3386109
user3386109

Reputation: 34829

Assuming the cutoff is 9am, all you need to do is create an NSDate that's 9 hours earlier than the actual time.

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:-(9 * 3600)];

That way every day begins and ends at 9am.

Upvotes: 0

Sanjay Changani
Sanjay Changani

Reputation: 77

You can use interval to specific date like

[[NSDate date] dateByAddingTimeInterval:interval];

Upvotes: 1

Related Questions