moment-db
Moment implementation for formatting Dates into ANSI compatible strings suitable for database consumption.
npm install moment-db
import MomentDB from 'moment-db.js'
Assume the date/time notation below.
Notation | Description |
---|---|
YYYY | 4-digit year |
MM | 2-digit month (01 to 12) |
DD | 2-digit day (01 to 31) |
HH | 2-digit hour (00 to 23) |
MI | 2-digit minute (00 to 59) |
SS | 2-digit second (00 to 59) |
FFF | Fraction of a second (1 to 9 digits) |
TH | 2-digit hour offset (-12 to 14) |
TM | 2-digit minute offset (00 to 59) |
YEARS | Number of years (max of 9999) |
DAYS | Number of days (max of 3652047) |
Using the forementioned notation, the following database data types can be used to construct ANSI compliant date/time formats:
Data Type | Format | Example |
---|---|---|
DATE | YYYY-MM-DD |
2030-01-31 |
TIME (w/o time zone) | HH:MI:SS.FFF |
12:01:20.903 |
TIME (with time zone) | HH:MI:SS.FFF [+|-]TH:TM |
12:01:20.903 -07:00 |
TIMESTAMP (w/o time zone) | YYYY-MM-DD HH:MI:SS.FFF |
2030-01-31 12:01:20.903 |
TIMESTAMP (with time zone) | YYYY-MM-DD HH:MI:SS.FFF [+|-]TH:TM |
2030-01-31 12:01:20.903 -07:00 |
INTERVAL YEAR TO MONTH | [+|-]YEARS-MM |
+130-01 |
INTERVAL DAY TO SECONDS | [+|-]DAYS HH:MI:SS.FFF |
-47482 12:01:20.903 |
Converting ES Dates
Each static MomentDB
function accepts a Date
that can be formatted into an ANSI compliant date/time format suitible for database consumption.
DATE
const date = MomentDB;// assumming date, the output would be something like:// 2030-01-31
TIME (w/o time zone)
const time = MomentDB;// assumming date, the output would be something like:// 12:01:20.903
TIME (with time zone)
const time = MomentDB;// assumming date/timezone, the output would be something like:// 12:01:20.903 -07:00
TIMESTAMP (w/o time zone)
const ts = MomentDB;// assumming date, the output would be something like:// 2030-01-31 12:01:20.903
TIMESTAMP (with time zone)
const ts = MomentDB;// assumming date/timezone, the output would be something like:// 2030-01-31 12:01:20.903 -07:00
INTERVAL (year to month)
const date1 = Date date2 = ;const monthDate = ;monthDate;const interval = MomentDB;// output (assuming we are in year 2030):// +130-01
INTERVAL (day to second)
const date1 = date2 = Date;const timeDate = ;const interval = MomentDB;// output (assuming we are in year 2030 and the time is the as indicated in timeDate):// -47482 12:01:20.903
Converting formatted dates/times into ES Dates
Each static MomentDB
function accepts a string returned from prior call to one of the string conversion functions that will be converted back into a Date
.
DATE
const date = MomentDB;const iso = date;// outputs a Date with the date set as UTC// 2030-01-31T00:00:00.000Z
TIME (w/o time zone)
const time = MomentDB;const iso = time;// outputs a Date with the time set as UTC// iso output (date defaults to 0000-01-01):// 0000-01-01T12:01:20.903Z // can also remove the timezone, if needed:const time2 = MomentDB;const iso2 = time2;// outputs a Date with the time set as UTC (no timezone)// iso2 output (date defaults to 0000-01-01):// 0000-01-01T12:01:20.903Z
TIME (with time zone)
const time = MomentDB;const iso = time;// outputs a Date with the time set with the indicated zone// iso output (date defaults to 0000-01-01):// 0000-01-01T19:29:38.903Z
TIMESTAMP (w/o time zone)
const ts = MomentDB;const iso = ts;// outputs a Date with the date/time set as UTC// iso output:// 2030-01-31T12:01:20.903Z // can also remove the timezone, if needed:const ts2 = MomentDB;const iso2 = ts2;// outputs a Date with the date/time set as UTC (no timezone)// iso2 output:// 2030-01-31T12:01:20.903Z
TIMESTAMP (with time zone)
const ts = MomentDB;const iso = ts;// iso output:// 2030-01-31T19:01:20.903Z
INTERVAL (year to month)
const date = MomentDB;const iso = date;// iso output (assuming the current year is 2030):// 2160-02-01T00:00:00.000Z // can also pass a reference date for the year instead of the default current dateconst date2 = MomentDB;const iso = date;// iso output (assuming today is 2030-02-03 and time in UTC is as indicated):// 2160-02-01T00:00:00.000Z
INTERVAL (day to second)
const date = MomentDB;const iso = date;// iso output (assuming today is 2030-02-03 and time in UTC is as indicated):// 1900-02-03T12:01:20.903Z // can also pass a reference date to use instead of the default current dateconst date2 = MomentDB;const iso = date;// iso output (assuming today is 2030-02-03 and time in UTC is as indicated):// 1900-02-03T12:01:20.903Z