+4 votes
209 views
in Web development by (242k points)
reopened
ISO 8601: unambiguous time expressions

1 Answer

+5 votes
by (1.6m points)
edited
 
Best answer

Basic principles of ISO 8601
Advantages of the ISO 8601 standard over other common conventions
Convert data to and from ISO format

image

ISO 8601: unambiguous time expressions

Temporary indications are a key element to coordinate and reach agreements. In the age of digital globalization, however, it is not easy to make them understandable to everyone. In the world there are regions and cultures with different conventions when it comes to temporal data, which puts at stake the precision and reliability of dates and times when communication is international. Precisely to solve this problem exists the ISO 8601 standard, a global standard that allows people in different countries to coordinate without misunderstandings..

Definition: ISO 8601

The international standard ISO 8601 gives recommendations for numerical date formats and other temporal expressions.

You can convert dates to the ISO date format you want, either using different tools or using a script. In this article we will tell you everything you need to know about this standard for temporary data..

Index
  1. Basic principles of ISO 8601
  2. Advantages of the ISO 8601 standard over other common conventions
  3. Convert data to and from ISO format

Basic principles of ISO 8601

The ISO 8601 standard establishes an internationally recognized format for presenting data: the form hours-minutes-seconds or, for dates, the form year-month-day . This structure allows you to place the figures directly one after the other, but you can also use hyphens to separate them, if you want to facilitate their reading. Thus, in the basic format of ISO 8601, the date September 7, 2019 would have the form 20190907 or, in its hyphenated version, 2019-09-07 . Expressions for hours are divided into hours, minutes and seconds, so in ISO 8601 they have the form 12:07:22 . With these ISO date formats (also called ISO date formats), country-specific conventions become dispensable, at least in electronic media . The different expressions are thus unified and cause less confusion.

Another designation for the ISO 8601 standard is EN 28601 (in its form for EU standards). This format not only serves to indicate points in time , but also periods : for this, the starting time or date is marked with the letter P to separate it from the next time data. Therefore, a period starting on September 6, 2019 at 8:00 p.m. and lasts a month, 5 days, and 3 hours, for example, would have the form 2019-09-06T20P1M5D3H in the unified ISO format..

All data, such as times, dates or periods, follows a specific order in the ISO standard: from the largest to the smallest unit, that is, in descending order . Thus, following the natural scale of the value of each unit, a larger unit will always appear before a smaller one. The advantage of this system is that it leads to the same result whether the data is classified according to lexicographic criteria, as if it is done chronologically.

Temporary elements of ISO 8601 (ISO date) in table form:

Representation according to ISO 8601 Possible values
Year and) yyyy, four digits or two in abbreviated mode
Month (M) MM, from 01 to 12
Week (W) ww, from 01 to 53
Day D) d, day of the week, 1 to 7
Hour (H) HH, from 00 to 23, 24:00:00 last hour
Minute (m) mm, from 00 to 59
Seconds) ss, from 00 to 59
Fraction (f) Fractions of a second, with the precision you want

In all the formats of the standard, the values ​​are classified in units and subunits: year , month , day , hour , etc. Each of the units contains a fixed number of digits. The natural sciences, software development and international correspondence are areas where this standard is particularly useful in reducing the risk of error in time indications. The so-called timestamp or ISO 8601 time stamp, internationally standardized , clearly reduces this risk.

After the date and time values, the difference with respect to coordinated universal time or, for short, UTC is usually added. It is a way to take into account possible differences between slots or summer schedules by country, using for this purpose usually the character Z .

Examples of time indications with time bands according to ISO 8601:

Example Explanation
2019-09-07T-15: 50 + 00 15:50 on 07.09.2019 in the time slot of coordinated universal time.
2019-09-07T15: 50 + 00Z Also 15:50 on 07.09.2019 in the time slot of coordinated universal time, with the addition Z in writing.
2019-09-07T15: 50 + 01: 00 15:50 on 07.09.2019 in the Spanish time zone (CET).

It is important to note that the validity period for the years in this format only ranges from 1583 to 9999 , since the Gregorian calendar was introduced in 1583. However, there is the possibility of applying the ISO 8601 standard to dates or indications prior to 1583, it is sufficient to first clarify with the parties involved in the communication how the equivalence will be carried out.

Advantages of the ISO 8601 standard over other common conventions

ISO 8601 regulates only numerical data and does not include temporal data in the expression of which words are used. It is not intended, therefore, to replace the formulas specific to each language, such as February 4, 1995 , but to avoid 2.4.95 expressions like (American form) or 9:30 .

The advantages offered by the ISO 8601 standard can be summarized in the following points:

  • Easy to read and write using software .
  • Easy to classify by simple comparison of character strings.
  • Understandable beyond language barriers.
  • Unmistakable among other common date formats.
  • Consistent with the usual 24-hour time format, in which the largest units (hours) also precede the smallest (minutes and seconds).
  • Strings that contain a date followed by the timestamp are also easy to compare and sort (for example, 2019-09-07 20:15:00 ).
  • Short and constant-length notation, which makes it easy to type and structure it in tables.
  • Identical to the Chinese date format, with which the largest cultural group on the planet (> 25%) is already familiar with it.
  • Also the date formats that follow the year-month-day order are very widespread: they are already common in Japan, Korea, Hungary, Sweden, Finland, Denmark, among other countries.
  • A four-digit value for the years avoids problems at the turn of the century.

Convert data to and from ISO format

Naturally, one can speak of the conversion of the format in two directions : a date or temporary data in the usual format of a region can be converted to the ISO format, but data in ISO format can also be expressed in the specific form of a certain region .

In the conversion that starts from an ISO date (ISO date) and gives it an easier or readable form, the timestamp of ISO 8601 usually has the following form:

  String dateString = "2019-09-26T07:58:30.996+0200"  

This string still has the form yyyy-MM-dd . The symbol that separates the date and time is T and the time format is HH: mm: ss plus the reference to UTC, .sssz . The integer format therefore has the following form: yyyy-MM-dd T HH: mm: ss.SSSZ .

The conversion we want to perform will transform this ISO expression, for example, to the typical Spanish format, that is: HH: mm, dd.MM.yyyy (hours, minutes; day, month, year).

  public String getOurDate() { DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); try { Date unformatedDate = format.parse(date); String formatedDate = new SimpleDateFormat("HH:mm dd.MM.yyyy").format(unformatedDate); return formatedDate; } catch (ParseException e){ System.out.println("Error") } return date; }  
advice

An alternative to manual conversion is offered by some websites to automatically transform the data to the desired format. An example of this is DenCode Converter, which allows you to directly enter the date in the initial format and then choose in which format you want to convert. You can also choose the time slot.


...