This documentation describes the use of Luxon DateTime handling for FHIR PrimitiveType classes for the FHIR
primitive date, dateTime, and instant data types.
The FHIR specification defines datetime primitive data types represented by strings of specific ISO 8601 variations.
Where a timezone offset (+zz:zz/-zz:zz) is specified, UTC (Z) may also be specified.
FHIR Primitive DateTime Formats:
YYYYYYYY-MMYYYY-MM-DDYYYYYYYY-MMYYYY-MM-DDYYYY-MM-DDThh:mm:ss+zz:zz / YYYY-MM-DDThh:mm:ssZYYYY-MM-DDThh:mm:ss.sss+zz:zz / YYYY-MM-DDThh:mm:ss.sssZYYYY-MM-DDThh:mm:ss.sss+zz:zz / YYYY-MM-DDThh:mm:ss.sssZThe Luzon library is the de-facto datetime handling library for JavaScript. It is a powerful, modern, and friendly wrapper for JavaScript dates and times. It provides support for datetime, duration, and interval that are immutable with native time zone and Intl support (no locale or tz files). The provided APIs are unambiguous and chainable. Additionally, Luxon supports handling for time zones and offsets (Time zones and offsets).
The following Luxon capabilities are used to support FHIR datetime-related primitives.
const dt: DateTime = DateTime.fromISO("2016");const dt: DateTime = DateTime.fromISO("2016-05");const dt: DateTime = DateTime.fromISO("2016-05-25");const dt: DateTime = DateTime.fromISO("2016-05-25T09:24:15Z");const dt: DateTime = DateTime.fromISO("2016-05-25T09:24:15-04.00");const dt: DateTime = DateTime.fromISO("2016-05-25T09:24:15.123Z");const dt: DateTime = DateTime.fromISO("2016-05-25T09:24:15.123-04.00");const dt: DateTime = DateTime.fromISO("2016-05-25T09:24:15Z");const dt: DateTime = DateTime.fromISO("2016-05-25T09:24:15-04.00");const dt: DateTime = DateTime.fromISO("2016-05-25", { zone: "utc" });const dt: DateTime = DateTime.fromISO("2016-05-25", { zone: "America/New_York" });DateTime.now().toISO() will default to the system's local datetime and timezone in ISO formatDateTime.utc().toISO() will default to UTC and timezone in ISO formatdt.toISO(); //=> '2017-04-20T11:32:00.000-04:00'dt.toISO({ suppressMilliseconds: true }); //=> '2017-04-20T11:32:00-04:00'dt.toISODate(); //=> '2017-04-20'dt.toFormat("yyyy"); //=> '2017'dt.toFormat("yyyy-MM"); //=> '2017-04'The DateTimeUtil namespace provides convenience wrapper functions for Luxon DataTime parsing and formatting to
support FHIR specified datetime primitive data types.
These are available for independent datetime handling use cases.
They have also been incorporated in the FHIR PrimitiveType DateType (implements interface DateTypeImpl),
DateTimeType (implements interface DateTimeTypeImpl), and InstantType (implementsinterface InstantTypeImpl)
implementations as convenience methods.
Refer to the Data Models documentation for the above PrimitiveType classes and their respective interfaces for additional information.
The DateTimeUtil.getDateTimeObject(...) and DateTimeUtil.getDateTimeObjectAsUTC(...) creation methods return a
valid Luxon DataTime object that allows full access to the Luxon DataTime APIs for virtually any kind of datetime
handling.
The following methods take a Luxon DateTime object as the method argument which is formatted appropriately into the
allowed FHIR primitive format and returned as a string:
DateTimeUtil.getValueAsYearDateTimeUtil.getValueAsYearMonthDateTimeUtil.getValueAsDateOnlyDateTimeUtil.getValueAsDateTimeDateTimeUtil.getValueAsInstantThe Luxon library supports handling for time zones and offsets.
The recommended approach is to use the DateTimeUtil.getDateTimeObjectAsUTC(...) creation method.
It automatically converts the provided ISO 8601 datetime string into UTC and returns a UTC DateTime object.
The DateTimeUtil.getDateTimeObject() creation method provides an optional opts parameter to control the creation
of the DateTime object (see fromISO(text, opts)
opts parameter for supported options).
In particular, refer to opts.zone and opts.setZone options for managing time zones and offsets.
The following use cases are supported:
opts parameter NOT provided or opts.zone and opts.setZone options NOT specified
Z):
opts parameter provided with opts.setZone option set to true
Z):
opts parameter provided with opts.zone option set to a valid time zone
Z):