ISO 8601

From Lazarus wiki
Revision as of 17:44, 26 September 2018 by Djzepi (talk | contribs) (Created page with "{{ISO 8601}} 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:...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

English (en) suomi (fi) русский (ru)

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:

 YYYY-MM-DD

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.

The time is recorded:

 HH:MM:SS

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.

The combined date and time should be marked with the letter "T":

 YYYY-MM-DDTHH:MM:SS

But often the intermediate letter "T" can be replaced by a space.


The time zone can be indicated by marking the time difference between the coordinated universal time (UTC) as follows:

 YYYY-MM-DDTHH:MM:SS±HH:MM


Useful functions

Function Description Unit
DayOfTheMonth Gives day of month index for a TDateTime value DateUtils
DayOfTheWeek Gives day of week index for a TDateTime value DateUtils
DayOfTheYear Gives the day of the year for a TDateTime value DateUtils
FormatDateTime Gives a string representation of a TDateTime value with a given format SysUtils
WeekOf Gives the week of the year for a TDateTime value DateUtils

Example

program iso_8601_project;

Uses SysUtils,DateUtils;


// Combined date and time in UTC  e.g // 2018-09-26T12:18:47Z
function  ISO8601_Combined_date_and_time_in_UTC(a_DateTime: TDateTime; a_Time_zone:string):string;
begin
  result := FormatDateTime( 'yyyy-mm-dd"T"hh:mm:ss', a_DateTime )+a_Time_zone;
end;

// Week: e.g. 2018-W39
function ISO8601_week(a_DateTime: TDateTime):string;
begin
  result := FormatDateTime('yyyy', a_DateTime)+'-W' +
    (WeekOf( a_DateTime )).ToString  ;
end;

//  Date with week number: e.g. 2018-W39-3
function ISO8601_Date_with_week_number(a_DateTime: TDateTime):string;
begin
  result := FormatDateTime('yyyy', a_DateTime)+'-W' +
    (WeekOf(a_DateTime)).ToString + '-' + (DayOfTheWeek(a_DateTime)).ToString ;
end;

// Ordinal date:  e.g. 2018-269
function ISO8601_Ordinal_date(a_DateTime: TDateTime):string;
begin
  result := FormatDateTime('yyyy', a_DateTime)+ '-'
     + (DayOfTheYear( a_DateTime )).ToString ;
end;

var
  aDateTime: TDateTime;
begin
  aDateTime := now;
  WriteLn('Date: '+ FormatDateTime( 'yyyy-mm-dd', aDateTime ));  // 2018-09-26
  WriteLn('Combined date and time in UTC: ');
  WriteLn('  ' + ISO8601_Combined_date_and_time_in_UTC(aDateTime,'+00:00')); // 2018-09-26T12:18:47+00:00
  WriteLn('  ' +  FormatDateTime( 'yyyymmdd"T"hhmmss', aDateTime )+'Z'); // 20180926T121847Z
  Writeln('  ' + ISO8601_Combined_date_and_time_in_UTC(aDateTime,'Z')); //2018-09-26T12:18:47Z
  WriteLn('Week: '+ ISO8601_week(aDateTime) ); // 2018-W39
  WriteLn('Date with week number: '+ ISO8601_Date_with_week_number(aDateTime)); // 2018-W39-3
  WriteLn('Ordinal date: '+ ISO8601_Ordinal_date(aDateTime)); // 2018-269
  ReadLn;
end.