From 866d3de646b3846931acdb92604670e276e6fb70 Mon Sep 17 00:00:00 2001 From: Ranadeep Polavarapu Date: Tue, 6 Oct 2020 20:45:28 -0400 Subject: [PATCH 1/3] 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" +``` From ccdb1019bb4af025067b2fc5115d0db470f3a80b Mon Sep 17 00:00:00 2001 From: RanadeepPolavarapu <7084995+RanadeepPolavarapu@users.noreply.github.com> Date: Tue, 6 Oct 2020 20:47:12 -0400 Subject: [PATCH 2/3] Update getISO8601StringTzAware.md --- snippets/getISO8601StringTzAware.md | 1 - 1 file changed, 1 deletion(-) diff --git a/snippets/getISO8601StringTzAware.md b/snippets/getISO8601StringTzAware.md index a17ad3420..5ff8833fe 100644 --- a/snippets/getISO8601StringTzAware.md +++ b/snippets/getISO8601StringTzAware.md @@ -18,7 +18,6 @@ const getISO8601StringWithTz = (d) => { 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 ( From 6b258bd6e1baea4df422e84d225396eb02b9f28d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 7 Oct 2020 09:25:05 +0300 Subject: [PATCH 3/3] Update and rename getISO8601StringTzAware.md to toISOStringWithTimezone.md --- snippets/getISO8601StringTzAware.md | 45 ----------------------------- snippets/toISOStringWithTimezone.md | 30 +++++++++++++++++++ 2 files changed, 30 insertions(+), 45 deletions(-) delete mode 100644 snippets/getISO8601StringTzAware.md create mode 100644 snippets/toISOStringWithTimezone.md diff --git a/snippets/getISO8601StringTzAware.md b/snippets/getISO8601StringTzAware.md deleted file mode 100644 index 5ff8833fe..000000000 --- a/snippets/getISO8601StringTzAware.md +++ /dev/null @@ -1,45 +0,0 @@ ---- -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)); - 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" -``` diff --git a/snippets/toISOStringWithTimezone.md b/snippets/toISOStringWithTimezone.md new file mode 100644 index 000000000..35016cbcd --- /dev/null +++ b/snippets/toISOStringWithTimezone.md @@ -0,0 +1,30 @@ +--- +title: toISOStringWithTimezone +tags: date,math,intermediate +--- + +Returns a string in simplified extended ISO format (ISO 8601), including timezone offset. + +- Use `Date.prototype.getTimezoneOffset()` to get the timezone offset and reverse it, storing its sign in `diff`. +- Define a helper function, `pad`, that normalizes any passed number to an integer using `Math.floor()` and `Math.abs()` and pads it to 2 digits, using `String.prototype.padStart()`. +- Use `pad` and the built-in methods in the `Date` prototype to build the ISO 8601 string with timezone offset. + +```js +const toISOStringWithTimezone = date => { + const tzOffset = -date.getTimezoneOffset(); + const diff = tzOffset >= 0 ? '+' : '-'; + const pad = n => `${Math.floor(Math.abs(n))}`.padStart(2, '0'); + return date.getFullYear() + + '-' + pad(date.getMonth() + 1) + + '-' + pad(date.getDate()) + + 'T' + pad(date.getHours()) + + ':' + pad(date.getMinutes()) + + ':' + pad(date.getSeconds()) + + diff + pad(tzOffset / 60) + + ':' + pad(tzOffset % 60); +}; +``` + +```js +toISOStringWithTimezone(new Date()); // "2020-10-06T20:43:33-04:00" +```