Update and rename getISO8601StringTzAware.md to toISOStringWithTimezone.md
This commit is contained in:
@ -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"
|
|
||||||
```
|
|
||||||
30
snippets/toISOStringWithTimezone.md
Normal file
30
snippets/toISOStringWithTimezone.md
Normal file
@ -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"
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user