From 866d3de646b3846931acdb92604670e276e6fb70 Mon Sep 17 00:00:00 2001 From: Ranadeep Polavarapu Date: Tue, 6 Oct 2020 20:45:28 -0400 Subject: [PATCH] feat: add getISO8601StringWithTz snippet Signed-off-by: Ranadeep Polavarapu --- snippets/getISO8601StringTzAware.md | 46 +++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 snippets/getISO8601StringTzAware.md diff --git a/snippets/getISO8601StringTzAware.md b/snippets/getISO8601StringTzAware.md new file mode 100644 index 000000000..a17ad3420 --- /dev/null +++ b/snippets/getISO8601StringTzAware.md @@ -0,0 +1,46 @@ +--- +title: getISO8601StringTzAware +tags: browser,node,date +--- + +Fetches an ISO8601 compliant datetime string with timezone awareness. + +_NOTE_: Use `Date.prototype.toISOString()` for UTC/GMT non-tz aware ISO8601 datetime string + +```js +/** + * Fetch ISO8601 compliant date string with tz offset. + * Use `Date.prototype.toISOString()` for UTC/GMT non-tz + * datetime string. + */ +const getISO8601StringWithTz = (d) => { + const tzo = -d.getTimezoneOffset(); + const dif = tzo >= 0 ? '+' : '-'; + const pad = (num) => { + const norm = Math.floor(Math.abs(num)); + // tslint:disable-next-line:no-magic-numbers + return (norm < 10 ? '0' : '') + norm; + }; + return ( + d.getFullYear() + + '-' + + pad(d.getMonth() + 1) + + '-' + + pad(d.getDate()) + + 'T' + + pad(d.getHours()) + + ':' + + pad(d.getMinutes()) + + ':' + + pad(d.getSeconds()) + + dif + + pad(tzo / 60) + + ':' + + pad(tzo % 60) + ); +}; +``` + +```js +getISO8601StringWithTz(new Date()); // "2020-10-06T20:43:33-04:00" +```