40

ISO 8601 represents a date as YYYY-MM-DD.

It doesn't seem to offer any recommendation on how to represent a date range, for example:

2013-01-01 => 2013-06-31

Does ISO 8601, or another standard, give a sensible recommendation for representing date ranges?

Background: this is to be used as the output of the toString() method of a DateRange object, output which could then be parsed with a parse() method.

Ben Smith
  • 19,589
  • 6
  • 65
  • 93
BenMorel
  • 34,448
  • 50
  • 182
  • 322

2 Answers2

43

ISO 8601 does have a standard for representing date ranges. To represent the start and end date using this format you would write:

2013-01-01/2013-06-31

Note how the forward slash is used as the interval designator to separate the start and end dates.

See this Wikipedia page for more information.

Ben Smith
  • 19,589
  • 6
  • 65
  • 93
  • 3
    If the leading part of the date is the same, you can omit it in the end date. So in this example the year is the same and it can be written as `2013-01-01/06-31`. – V02460 May 09 '18 at 08:35
22

Just to add to Ben Smith's answer above, the standard also mentions using a double hypen (--) as the interval designator in certain contexts, instead of the forward slash (/). For example, in file or directory names where using a forward slash is forbidden in some operating systems (e.g. Windows).

So the above examples would be

2013-01-01--2013-06-31

or

2013-01-01--06-31

in abbreviated form

Tim
  • 1,839
  • 10
  • 18
  • Is the abbreviated form actually a valid ISO8601? `--06-31` would be a valid date but if we use `--` as designator then the date here is written as `06-31` which doesn't seem valid. – Jezor Dec 07 '20 at 11:12
  • 4
    @Jezor the link in Ben Smith's answer (https://en.wikipedia.org/wiki/ISO_8601#Time_intervals) seems to say so: "For / expressions, if any elements are missing from the end value, they are assumed to be the same as for the start value including the time zone." So it's not a valid date in and of itself, but it is valid as the end of a date range. – Tim Dec 07 '20 at 16:04