fbpx

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

02-04-10. What does that mean? If you’re from the US, then it probably means February 4th, 2010. But it might also mean the 2nd of April, 2010. Or is it April 10th, 1902?

There’s a group called the International Organization for Standardization and has been operating in its current form since 1947. Most people refer to the group as ISO and refer to various standards that the group produces as ISO followed by some number. The standard for dates has gone through several versions and is now known as the ISO 8601 standard. The current version is ISO 8601:2004

Listen to the full episode about dates and times, or you can also read further for the full transcript below.

Transcript

02-04-10. What does that mean? If you’re from the US, then it probably means February 4th, 2010. But it might also mean the 2nd of April, 2010. Or is it April 10th, 1902?

Not only are there different ways to display a date, there’s different ways to store and represent a date. Many of them are based on a time as well as a date. I’ll explain times in the next episode which should put everything together into the DateTime type. This episode will focus more on the details you’ll need, to understand the date portion of DateTimes. You’ll learn about standards, calendars and different ways to think about dates. We’ll get to the actual DateTime data type in the next episode.

There’s a group called the International Organization for Standardization or as it’s normally known, I, S, O, or ISO and has been operating in its current form since 1947. If you’re wondering why the order of the letters doesn’t match the name, that’s something I’d like to know too. I always thought it was because the original name was in a different language that did match the letters. But that doesn’t seem correct.

Whatever the name, most people refer to the group as ISO and refer to various standards that the group produces as ISO followed by some number. The standard for dates has gone through several versions and is now known as the ISO 8601 standard.

It can sometimes be difficult to read a standard document because the language has to be very specific. Documents like this sometimes leave out aspects that I consider important and then define something else in such detail that I find it difficult to stay awake. If you’re the same way, then let me help by lending my interpretation of this standard.

When we write a time, we’re already used to the hour coming before the minute which comes before the second. This is beneficial in many ways including that it helps sort things. The standard defines a similar ordering for dates that also starts with the biggest unit, then the next smaller unit, all the way to the smallest unit. For dates, this means that the year comes first, then the month, and then the day.

Why do we need months though? I mean, they’re sort of arbitrary. Maybe at some point in history, they referred to the cycles of the moon but now we learn about the months, their order, and the number of days in each through a children’s rhyme. Or you can use my favorite, the knuckle test. Why does August get 31 days? Why not take a day from a couple of the longer months and give them to February. It always seemed a bit unfair to me that February was so short.

We normally use the Gregorian calendar which defines months more out of tradition than anything else. And it’s based on a Christian event that defines the beginning of the calendar. This is not the only calendar system in the world. There are other systems that are used extensively and in everyday matters. Some of them are based on the cycles of the moon which at least makes the concept of a month important and relevant.

Because we have different calendars each with different starting points in history used widely by different countries and people, it’s super important to have one system that everybody agrees on. Since we don’t yet have stardates, adopting a standard around the Gregorian calendar is at least a step in the right direction. The alternative would be to constantly have to convert dates and times between calendars. Believe me, just one calendar has enough complexities. Be glad that we have a standard. I’ll explain more right after this message from our sponsor.

( Message from Sponsor )

I started explaining how the standard specifies that the year comes first, then the month, and then the day. It’s actually more specific than just that. First of all, the year must be written as four digits. There’s no more mistaking 50 for either 1850, 1950, or 2050. and the months and days must both be written as two digits with a leading zero when necessary. So you don’t just write a 1 for January. It has to be a 01.

And along the same lines, the standard avoids naming the months. This is because dates when spelled out with the month names should be localized into different languages with the local name of each month for that language. By using only the Arabic numbers, we avoid any localization issues.

That doesn’t mean that the dates have to be just numbers only. There can be a dash between the year and the month and between the month and the day. You should be consistent. Don’t put a dash between the year and the month but then leave out the dash between the month and the day.

What happens if you want to be less precise about the date. Can you leave part of it out completely? Yes but this also has rules. You can leave off the day and just have a year and a month. Or you can also leave off both the day and month and just have a year. But you can’t leave off the month only or leave off the year only. You also can’t leave off the year and month to have just a day. And to be absolutely complete, you can’t leave off the year and day and have just a month. By following these rules, you’ll notice that whatever you leave off will result in a date that’s still unambiguous. In other words, the result can’t be confused. That’s a good thing because it avoids the biggest problem we had before this standard which was what does the date 02-04-10 mean.

Because dates always start with a four digit year and then can have a 2 digit month and then can have a 2 digit day, there’s no misunderstanding.

Alright so back to those months again. In some computer systems, months can be a distraction. So why not get rid of them? We can! And the ISO 8601 standard provides two ways to do this.

Think about weeks for a moment. The week is a duration of seven days that’s still widely used. The only real source of confusion is what day of the week should come first? Common choices are Sunday, Monday, and even Saturday. Well, we need to pick one and the standard says Monday is the first day of the week. All we need to do then is to number each week of the year from 01 up to a maximum of 53. Remember to use a leading 0 for weeks 01 through 09.

If you want to refer to the first week of the year 2020, then what’s to prevent 2020-01 meaning the first week from being confused with 2020-01 meaning the first month? The standard says that in order to use week numbers, then you also need to place a capital W before the week number. You can do this with or without dashes. That means if you really want to refer to the first week in the year 2020, then the proper way to write it is 2020-W01 or just 2020W01.

Now, weeks have one more complexity. I told you dates were complicated. And we haven’t even started with times yet. The problem is that new years don’t always begin on a Monday. What do we do when a new year begins on some day other than the first day of the week? Well, we don’t use a -1 for the days before the first Monday. I’m really glad the ISO standard stayed away from that solution. Weeks are always positive. But this means that there could be some days at the beginning of a new year that actually belong to the last week from the previous year. The standard says that the first week begins with whatever week will have the majority of the days in the new year. This also means it’s possible for a few of the days from the previous year to end up lumped in with the first week in the new year. Whenever you have a week that spans a new year, then the standard just says that week will belong to whatever year has more of its days. So if a week has 4 or more of its days in the previous year, then it belongs in the previous year. And if it has 4 or more of its days in the new year, then it belongs in the new year.

Now that you know how to define a specific week, how do you refer to a particular day in this system? If you want to, you can just refer to the week and leave the day unspecified. But if you do need to be precise, then just add a single digit either with or without a dash to represent the day. Remember to be consistent with the dashes. For example, to refer to the Wednesday which is defined to be the third day in the second week of the year 2020, you could write either 2020-W02-3 or as 2020W023 without dashes.

I’ll end this episode with the final way to represent a date. In a way, this is the easiest of them all. But it’s not something we’re used to. In this system, each day is numbered starting with 1 and going up to either 365 or 366 for leap years. You still need to use leading zeros. Only this time, because the maximum day number uses 3 digits, then the days should always use three digits. For example, the 3rd day of the year will be number 003, and the 50th day will be number 050. Because the number of days uses three digits, there’s no confusing them with months. We don’t need a special letter like we did with weeks. To write the 3rd day of the year 2020 will then be, 2020-003 or 2020003 without dashes.