# Python: ISO 8601 date

This blog talks about applying ISO 8601 date by python such as ISO year, ISO week, ISO date; how to convert datetime format to ISO format and how to convert ISO format to datetime format.

ISO 8601, an international standard covering the exchange of date and time-related data, as the string format. The ISO calendar is a widely used variant of the Gregorian calendar. The ISO year consists of 52 or 53 full weeks, and where a week starts on a Monday and ends on a Sunday. The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday. This is called week number 1, and the ISO year of that Thursday is the same as its Gregorian year.

• Basic ISO 8601 calendar elements
• Converting datetime.date to ISO 8601 format
• Parsing an ISO 8601-formatted date

## Basic ISO 8601 calendar elements

### ISO year & ISO week

The first week of year 2019 is from 31th December 2018 to 6th January 2019, we want the year of 31th December 2018 to be 2019, in this case we can apply isocalendar() in order to getting the ISO year, same for ISO weekday.

There are several mutually equivalent and compatible descriptions of week 01:

• the week with the year’s first Thursday in it (the formal ISO definition)
• the week with 4 January in it
• the first week with the majority (four or more) of its days in the starting year
• the week starting with the Monday in the period 29 December – 4 January

Date 2018-12-31’s year is 2018, but its ISO year is 2019. In this case, we can apply isocalendar() to achieve it.

### ISO weekday

isoweekday() returns different results from weekday(), it presents value 1 as Monday.

## Converting date to ISO 8601 format

Calendar date includes 3 principal elements: YYYY, MM and DD. YYYY indicates a four-digit year, 0000 through 9999, MM indicates a two-digit month of the year, 01 through 12, DD indicates a two-digit day of that month, 01 through 31.

As the example above, we need to pay attention one-digit month or day value is different between datetime.date value and isoformat value. ISO month and day are always two-digit value.

## Parsing an ISO 8601-formatted date

### date.fromisoformat()

Specifically, this function supports strings in the format(s) YYYY-MM-DD.

Caution: This does not support parsing arbitrary ISO 8601 strings - it is only intended as the inverse operation of date.isoformat().

### parser.parse()

dateutil.parser offers a generic date/time string parser which is able to parse most known formats to represent a date and/or time. parser.parse() parses a string in one of the supported formats.