date-vir
Easy dates and times with explicit timezones (and typed timezones).
Full docs: http://electrovir.github.io/date-vir
date-vir
revolves around its FullDate
type. FullDate
includes all the information you need to correctly pinpoint and format a given point in time. Most notably, FullDate
always includes a timezone
property, so you always know what timezone the date was originally defined in, or what timezone the date is intended to be used in:
import {FullDate, utcTimezone} from 'date-vir';
const myDate: FullDate = {
year: 2023,
month: 6,
day: 5,
hour: 5,
minute: 23,
second: 27,
millisecond: 652,
timezone: utcTimezone,
};
Creating a FullDate
Several functions are available for creating FullDate
objects:
import {
createFullDate,
getNowFullDate,
parseDateStringWithPattern,
parseInputElementValue,
Timezone,
} from 'date-vir';
createFullDate('2023-06-05', Timezone['Europe/Rome']);
getNowFullDate(Timezone['America/Argentina/Buenos_Aires']);
parseInputElementValue(document.querySelector('input'), Timezone['Asia/Tokyo']);
parseDateStringWithPattern({
dateString: '870-0-14-19 5 2023 6',
formatString: 'S-s-h-m d yyyy M',
timezone: Timezone['America/Cancun'],
});
Outputs
FullDate
objects are easily converted into other formats (usually strings) with the included functions:
import {
createFullDate,
formatPresets,
FullDatePart,
Timezone,
toHtmlInputString,
toLocaleString,
toTimestamp,
toUtcIsoString,
} from 'date-vir';
const myFullDate = createFullDate('2023-06-05T14:19:00.870Z', Timezone['America/Chicago']);
toTimestamp(myFullDate);
toUtcIsoString(myFullDate);
toLocaleString(myFullDate, formatPresets.DatetimeFull);
toHtmlInputString(myFullDate, FullDatePart.Date);
Timezone
date-vir
exposes a type-safe list of timezone names, including shortcuts for the user's timezone and the UTC timezone:
import {Timezone, userTimezone, utcTimezone} from 'date-vir';
Timezone['Africa/Abidjan'];
Timezone['America/Los_Angeles'];
Timezone['Etc/GMT+1'];
utcTimezone;
userTimezone;
Note that the current list of timezone names included in this package was generated from Firefox 106.0.2.
Luxon
The parsing and timezone conversions in date-vir
utilize the luxon
package under the hood. If you wish to gain full luxon
control over a FullDate
object, date-vir
provides an easy conversion function:
import {createFullDate, Timezone, toLuxonDateTime} from 'date-vir';
const myFullDate = createFullDate('2023-06-05T14:19:00.870Z', Timezone['America/Chicago']);
toLuxonDateTime(myFullDate);