How to normalize NSDate objects (i.e. set the time to midnight)

When you create new date using [NSDate date] (i.e. today, as in right now) your date will save the current time as well as its date.

In fact, under the hood an NSDate object is the amount of seconds that have elapsed since the 1st of January 2001 (or 1970), in milliseconds. So really it’s a massive floating point number. You can see what it is with this code:


To save you the trouble, it’s been roughly 1362585768.752258 seconds since 1970. But I digress…

Sometimes you want the date objects you’re dealing with not to regard “time” and really only deal with dates. As such, all dates – to be comparable – need to have the very same time, say midnight. Otherwise you may get the wrong results. Imagine adding three date objects that each have noon as their time, that’s an extra day and a half clouding your results right there.

Here’s some code that will normalize your NSDate object by setting its time to midnight:


In a nutshell, we “explode” the date into its year, month and day components, and then recreate a new object with these and no other components.

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.