ISO 8601: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | ISO 8601 is a standard issued by the International Organization for Standardization (ISO) for the date and time. According to ISO 8601, the date is as follows: | |
| 2 | 2 | ||
| 3 | - | ||
| 3 | + | ```text | |
| 4 | + | YYYY-MM-DD | |
| 5 | + | ``` | |
| 4 | 6 | ||
| 5 | 7 | where YYYY is a year in the Gregorian calendar , MM is a month between 01 (January) and 12 (December), and DD is the day of the month between 01 and 31. | |
| 6 | 8 | ||
| 7 | 9 | The time is recorded: | |
| 8 | 10 | ||
| 9 | - | ||
| 11 | + | ```text | |
| 12 | + | HH:MM:SS | |
| 13 | + | ``` | |
| 10 | 14 | ||
| 11 | 15 | where HH has hours after midnight between 00 and 23. The MM is minutes from the beginning of the previous hour between 00 and 59. The SS is seconds from the beginning of the previous minute between 00 and 59. | |
| 12 | 16 | ||
| 13 | 17 | The combined date and time should be marked with the letter "T": | |
| 14 | 18 | ||
| 15 | - | ||
| 19 | + | ```text | |
| 20 | + | YYYY-MM-DDTHH:MM:SS | |
| 21 | + | ``` | |
| 16 | 22 | ||
| 17 | 23 | But often the intermediate letter "T" can be replaced by a space. | |
| 18 | 24 | ||
| 19 | 25 | The time zone can be indicated by marking the time difference between the coordinated universal time (UTC) as follows: | |
| 20 | 26 | ||
| 21 | - | ||
| 27 | + | ```text | |
| 28 | + | YYYY-MM-DDTHH:MM:SS±HH:MM | |
| 29 | + | ``` | |
| 22 | 30 | ||
| 23 | 31 | ## Useful functions | |
| 24 | 32 | ||
| 25 | 33 | | Function | Description | Unit | | |
| 26 | 34 | |----|----|----| | |
| 27 | 35 | | DayOfTheMonth | Gives day of month index for a [[TDateTime]] value | DateUtils | | |
| 28 | 36 | | DayOfTheWeek | Gives day of week index for a TDateTime value | DateUtils | | |
| 29 | 37 | | DayOfTheYear | Gives the day of the year for a TDateTime value | DateUtils | | |
| 30 | 38 | | FormatDateTime | Gives a string representation of a TDateTime value with a given format | SysUtils | | |
| 31 | 39 | | WeekOf | Gives the week of the year for a TDateTime value | DateUtils | | |
| 32 | 40 | ||
| 33 | 41 | ## Example | |
| 34 | 42 | ||
| 35 | 43 | ``` pascal | |
| 36 | 44 | program iso_8601_project; | |
| 37 | 45 | ||
| 38 | 46 | Uses SysUtils,DateUtils; | |
| 39 | 47 | ||
| 40 | 48 | // Combined date and time in UTC e.g // 2018-09-26T12:18:47Z | |
| 41 | 49 | function ISO8601_Combined_date_and_time_in_UTC(a_DateTime: TDateTime; a_Time_zone:string):string; | |
| 42 | 50 | begin | |
| 43 | 51 | result := FormatDateTime( 'yyyy-mm-dd"T"hh:mm:ss', a_DateTime )+a_Time_zone; | |
| 44 | 52 | end; | |
| 45 | 53 | ||
| 46 | 54 | // Week: e.g. 2018-W39 | |
| 47 | 55 | function ISO8601_week(a_DateTime: TDateTime):string; | |
| 48 | 56 | begin | |
| 49 | 57 | result := FormatDateTime('yyyy', a_DateTime)+'-W' + | |
| 50 | 58 | (WeekOf( a_DateTime )).ToString ; | |
| 51 | 59 | end; | |
| 52 | 60 | ||
| 53 | 61 | // Date with week number: e.g. 2018-W39-3 | |
| 54 | 62 | function ISO8601_Date_with_week_number(a_DateTime: TDateTime):string; | |
| 55 | 63 | begin | |
| 56 | 64 | result := FormatDateTime('yyyy', a_DateTime)+'-W' + | |
| 57 | 65 | (WeekOf(a_DateTime)).ToString + '-' + (DayOfTheWeek(a_DateTime)).ToString ; | |
| 58 | 66 | end; | |
| 59 | 67 | ||
| 60 | 68 | // Ordinal date: e.g. 2018-269 | |
| 61 | 69 | function ISO8601_Ordinal_date(a_DateTime: TDateTime):string; | |
| 62 | 70 | begin | |
| 63 | 71 | result := FormatDateTime('yyyy', a_DateTime)+ '-' | |
| 64 | 72 | + (DayOfTheYear( a_DateTime )).ToString ; | |
| 65 | 73 | end; | |
| 66 | 74 | ||
| 67 | 75 | var | |
| 68 | 76 | aDateTime: TDateTime; | |
| 69 | 77 | begin | |
| 70 | 78 | aDateTime := now; | |
| 71 | 79 | WriteLn('Date: '+ FormatDateTime( 'yyyy-mm-dd', aDateTime )); // 2018-09-26 | |
| 72 | 80 | WriteLn('Combined date and time in UTC: '); | |
| 73 | 81 | WriteLn(' ' + ISO8601_Combined_date_and_time_in_UTC(aDateTime,'+00:00')); // 2018-09-26T12:18:47+00:00 | |
| 74 | 82 | WriteLn(' ' + FormatDateTime( 'yyyymmdd"T"hhmmss', aDateTime )+'Z'); // 20180926T121847Z | |
| 75 | 83 | Writeln(' ' + ISO8601_Combined_date_and_time_in_UTC(aDateTime,'Z')); //2018-09-26T12:18:47Z | |
| 76 | 84 | WriteLn('Week: '+ ISO8601_week(aDateTime) ); // 2018-W39 | |
| 77 | 85 | WriteLn('Date with week number: '+ ISO8601_Date_with_week_number(aDateTime)); // 2018-W39-3 | |
| 78 | 86 | WriteLn('Ordinal date: '+ ISO8601_Ordinal_date(aDateTime)); // 2018-269 | |
| 79 | 87 | ReadLn; | |
| 80 | 88 | end. | |
| 81 | 89 | ``` | |
| 82 | 90 | ||
| 83 | 91 | --- | |
| 84 | 92 | *From the Free Pascal wiki, page "ISO 8601", as it stood on 2019-07-08 after a change by Djzepi, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
