Dates and times are a lot more complicated than we normally realize. Having a specific data type to manage all the details is crucial.

This episode explains the following topics related to time:

  • What is UTC and the difference between coordinated universal time, UTC, and Greenwich Mean Time, GMT.
  • How to handle midnight and noon.
  • How to specify time zones and what it means when the time zone is unspecified.
  • How to work with and read aloud 24 hour time formats.
  • How to specify fractional time units. For example, how to write a time that includes microseconds.
  • How to work with leap seconds.
  • How to combine dates and times into a DateTime format.
  • How to specify date and time intervals.

Listen to the full episode or you can also read the full transcript below.


This episode continues the explanation of DateTime data types with the time portion. You might think, how hard can it be to tell the time? Like most other everyday topics, there’s a lot more to consider when the scope goes beyond your immediate geographical boundaries. In the case of times, getting this wrong can cause your program to reject valid dates or even crash. I was planning to finish this topic with this episode, but there’s still too much to cover. So today will explain how to represent times in a written format just like I explained for dates. The fact that it takes an entire episode just to explain how to write a time should give you some insight into the complexities involved.

First of all, we have time zones and they’re not all in one hour increments. Then there’ll be cases where the time is in coordinated universal time or UTC for short. This is another example where the letters don’t match the full name but at least here, we know why. UTC is the common and agreed on abbreviation for all languages.

Then we need to consider 24 hour clocks vs. using am and pm. And is there a difference between the times 24:00 and 00:00? Does midnight belong to the day before or the next day?

We all know about leap years where an extra day gets added to February. But did you know there are leap seconds?

It’s details like these that we tend to ignore or clarify in everyday use. The uncertainty and ambiguity have no place in a DateTime data type.

Okay, so how does the ISO 8601 standard say that times should be written to avoid any misunderstanding? The hours come first with two digits. If the time is 2 o’clock, don’t write just a 2, write a 02. The minutes are next also using 2 digits and then the seconds with 2 digits. You can have colons between the hours and minutes or not. Don’t use dashes like the date. And even if you live in a locale that normally uses dots, stick to the standard and use colons.

The reason colons are used is because any of the units of time, hours, minute, or seconds can have a fractional part after it and that fractional part begins with a decimal separator. The standard allows either a comma or a dot here. If dots were used to separate the units, then that could be confused with the beginning of a fractional part. Once you begin a fractional part, then the remaining units if any should not be specified. In other words, don’t try to say 1.5 hours and 3 minutes. That even sounds strange. There’s no limit for how many digits you can use for the fractional part. For example, if you wanted to represent one microsecond after 10 30, then that would be written as 10:30:00.000001.

Just like how you can leave of portions of the date to be less precise, you can also leave off portions of the time. Make sure to leave off the smallest units. This means you can’t have a time with just minutes. You can have a time with just hours. Or just hours and minutes. Or just hours, minutes, and seconds. Putting this together with the last point, if you wanted to represent 10 30 itself leaving off the seconds, then you could either write 10:30 or you could also write 10.5.

The reason all this must be specified so carefully is that programmers have traditionally been inclined to take shortcuts or come up with their own rules. Any alternate rules, or no rules at all, make it difficult if not impossible to accurately represent a time that another computer can then read.

There’s more though. Did you know that computers sometimes need to purposefully slow down time just a bit to work around poorly written software? I’ll explain more right after this message from our sponsor.

( Message from Sponsor )

Let’s say we have a time written as 10:30? What does that mean? Does that mean a specific time of 10 30 in the morning? Or does it mean a duration of ten and a half hours? The standard leaves that up to you to interpret however you want. If you know that you’re working with durations instead of specific points in time, then it means a duration.

When does a new day begin? Does it begin at midnight? Or right after midnight? Does midnight belong to the previous day? It might be surprising but you can actually think of it as if there are two midnights, one belonging to the previous day and one to the next. The times 00:00 and 24:00 both refer to midnight and both refer to the exact moment in time. When you want to refer to midnight as the end of a day, then use 24:00. And when you want to refer to it as the beginning of a day, then use 00:00. If you have no real reason to use one or the other, then the standard prefers 00:00.

And what about noon? This is really just a common term that’s used so that 12 hour formats that use am and pm don’t get confused about whether noon should be am or pm. There’s no good reason to continue using 12 hour formats other than it’s still common in the US.

The hour portion can go from 0 to 24. Now a normal digital clock will never show a time with 24 on the hours. It’ll go from 23:59 to 00:00. The only time you need to use 24 for the hour is when you want to include midnight in the previous day.

The minutes go from 00 to 59. And the seconds normally go from 0 to 59 too. But sometimes, the Earth doesn’t always spin at a constant rate. Believe it or not, the Earth can sometimes spin a little faster or slower. The difference is enough that sometimes a second has to be added or removed. This is done to the UTC time in order to keep it aligned with the position of the Earth and Sun. Well, whenever a second needs to be added, that means that the value of seconds can go beyond 59 and include 60. This can cause problems for software that’s not expecting this. So instead of adding an extra second and risk breaking software, some companies and maybe even governments will choose to instead slow down time for a bit to allow an extra second without actually counting up to 60 seconds.

The standard also give guidance on how to represent intervals. How would you go about displaying a meeting that goes from 1 o’clock in the afternoon until 2 o’clock pm? Notice how I keep needing to specify either in the morning or afternoon, or am or pm? Let’s say the meeting goes from 13 hours until 14 hours. There’s no fully agreed on way to pronounce 24 hour times. I’ll just read the hours followed by the word hours and then the minutes followed by the word minutes and the seconds followed by the word seconds. Some people prefer to say the word hundred before the word hours if the minutes are zero. Like in programming, I prefer to avoid special case rules like this, so I’ll try not to do that.

Alright, back to the interval. Use a forward slash character between the range. So you would write 13/14. And to show an interval of 30 minutes from 13 hours until 13 hours 30 minutes, you would write 13:00/13:30. The only problem is that this notation can be confused with a common interpretation to mean either 13 hours or 14 hours or for the second example to mean either 13 hours or 13 hours 30 minutes. You might be tempted to use a dash instead and write 13-14 but this has problems too. You see, intervals work not just on times but on dates too.

If you write 2010-12 hoping to represent the years 2010, 2011, and 2012, then that’s not what it means. 2010-12 means the month of December in the year 2010. If you really want this range, then use a slash and use the same number of digits for each year. the correct way to write this is 2010/2012.

Let’s switch topics for a moment and talk about time zones. If you have a time written such as 13:30, then this means 13 hours 30 minutes in your local time zone. So if you don’t specify otherwise, times refer to a local time. You can refer to a coordinated universal time or UTC time by placing a capital letter Z after the time. UTC is a global time that countries around the world agree to and make sure to keep their clocks synchronized with. That’s why it’s a coordinated time. UTC sometimes aligns with GMT and in fact, the term GMT used to be used when people wanted to refer to a common time. That’s not the case anymore and now GMT is just a timezone like any other local time zone. UTC is a time standard not a time zone.

If you want to represent a time without a timezone, then that’s best used for a time that should be interpreted by anybody viewing the time as applying to their time zone. Let’s say that you send an email to many people around the world asking them to spend an hour starting at 13 hours to work on some project. They’ll all interpret this time to be their local time and will be working on the project at different times around the world as 13 hours comes around to them.

If instead, you want everybody to join a meeting at the same instant, then schedule the meeting to start at 13:00Z or whatever time you want. By using UTC time, it’s clear that the time no longer refers to their local time.

You can also specify a time relative to UTC if a specific time zone is important for some reason. To do this, add either a plus or minus sign followed by a time. The time should use two digits for hours and can also include minutes. If it includes minutes, then you can use a colon or not to separate them. The minutes should also be two digits. The time that you use should be how far ahead or behind UTC the local time really is. For example, central european time is one hour ahead of UTC and eastern standard time is 5 hours behind UTC. The times 13:00Z, and 14:00+0100, and 08:00-05 all represent the same time.

The last thing I’ll explain today is how to combine dates and times and then I’ll put everything together into some examples showing intervals.

The standard likes to use capital letters like the capital W I explained for representing weeks, or the capital Z for UTC. Well, when you want to combine a date and time, you just put the date first, followed by a capital T, and then the time. You might see some dates and times written without the capital T and just use a space instead. That’s actually more of just a shortcut display format that displays a date and then a time. In order to really combine them into a single DateTime, you should use a capital T to signal that a time follows the date.

We can put all this together now. At least for the written representation of DateTimes. Let’s say you want to write an interval that goes from 13 hours UTC on July 1st, 2020 until 10 hours UTC the next day. You would write it like this:
◦ 2020-07-01T13Z/2020-07-02T10Z

Notice that I had to include the dates for both the beginning and end because the dates were different. If instead, you want an interval that goes from July 1st, 2020 at 13 hours until 14 hours on the same day, then you can just write it like this:
◦ 2020-07-01T13Z/14Z

For another example, let’s say that you’re not interested in times at all but just a multi-day event that goes from July 1st, 2020 until July 3rd. You would write that as:
◦ 2020-07-01/03