Update top JS snippets

This commit is contained in:
Chalarangelo
2021-10-13 20:09:28 +03:00
parent 2b775e3c93
commit 3cd481c0d1
19 changed files with 45 additions and 43 deletions

View File

@ -2,12 +2,12 @@
title: toISOStringWithTimezone
tags: date,intermediate
firstSeen: 2020-10-07T09:25:05+03:00
lastUpdated: 2020-10-22T20:24:30+03:00
lastUpdated: 2021-10-13T19:29:39+02:00
---
Converts a date to 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`.
- Use `Date.prototype.getTimezoneOffset()` to get the timezone offset and reverse it. Store 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.