feat: add toLocalISOString()

This commit is contained in:
AJ ONeal 2022-03-09 23:29:07 -07:00
parent 7acf670aa6
commit c3f690ae6d
No known key found for this signature in database
GPG Key ID: 585419CA6DB0AA23
4 changed files with 98 additions and 3 deletions

View File

@ -8,6 +8,13 @@ in ~100 LoC. For Node.js & Browsers.
XTZ is a poor man's `Temporal` polyfill, but just for time zones. \ XTZ is a poor man's `Temporal` polyfill, but just for time zones. \
Demo: <https://therootcompany.github.io/tz.js/> Demo: <https://therootcompany.github.io/tz.js/>
```js
// What's the current time, in ISO format?
TZ.toLocalISOString(new Date()); // "2021-11-07T03:15:59.000-0500"
TZ.timeZone(); // "America/New_York"
```
> What UTC time will it be when it's 3:15am in New York? > What UTC time will it be when it's 3:15am in New York?
```js ```js
@ -98,6 +105,7 @@ https://www.youtube.com/playlist?list=PLxki0D-ilnqa6horOJ2G18WMZlJeQFlAt
# API # API
- `toLocalISOString(dateOrNull)`
- `toTimeZone(utcDate, timeZone)` - `toTimeZone(utcDate, timeZone)`
- `toTimeZoneISOString(isoString, timeZone)` - `toTimeZoneISOString(isoString, timeZone)`
- `toUTC(dtString, timeZone)` - `toUTC(dtString, timeZone)`

View File

@ -86,6 +86,18 @@ String(((new Date()).getTimezoneOffset() % 60)).padStart(2, '0')</code></pre>
<hr /> <hr />
<form class="js-local">
<h3>Local ISO+Offset String:</h3>
<pre><code>XTZ.toLocalISOString(new Date())</code></pre>
<label>
ISO:
<input class="js-my-dt-local" type="text" disabled />
</label>
<br />
</form>
<hr />
<form class="js-tz2utc"> <form class="js-tz2utc">
<h3>Relative TimeZone to Absolute UTC:</h3> <h3>Relative TimeZone to Absolute UTC:</h3>
<pre><code>XTZ.toUTC("<span class="js-dtx-tz">YYYY-03-14 03:15:69.000</span>", "<span class="js-tzx-tz">UTC</span>") <pre><code>XTZ.toUTC("<span class="js-dtx-tz">YYYY-03-14 03:15:69.000</span>", "<span class="js-tzx-tz">UTC</span>")
@ -182,6 +194,7 @@ Asia/Kolkata +0530 (No DST) (30-min)</pre
>github.com/therootcompany/tz.js</a >github.com/therootcompany/tz.js</a
> >
<script src="./xtz.js"></script> <script src="./xtz.js"></script>
<script src="./tolocalisostring.js"></script>
<script> <script>
function $(sel, el) { function $(sel, el) {
return (el || document.body).querySelector(sel); return (el || document.body).querySelector(sel);
@ -222,6 +235,8 @@ Asia/Kolkata +0530 (No DST) (30-min)</pre
$(".js-my-dt-xtz").value = translate(curDt, inTz, outTz).toISOString(); $(".js-my-dt-xtz").value = translate(curDt, inTz, outTz).toISOString();
} }
$(".js-my-dt-local").value = XTZ.toLocalISOString();
var myTz = new Intl.DateTimeFormat("default", {}).resolvedOptions() var myTz = new Intl.DateTimeFormat("default", {}).resolvedOptions()
.timeZone; .timeZone;
$(".js-my-tz").innerText = myTz; $(".js-my-tz").innerText = myTz;

50
tolocalisostring.js Normal file
View File

@ -0,0 +1,50 @@
(function (exports) {
"use strict";
// See <https://github.com/coolaj86/AJScript/issues/27>
function toLocalISOString(dateOrStr) {
var d;
if (dateOrStr) {
d = new Date(dateOrStr);
} else {
d = new Date();
}
var YYYY = d.getFullYear();
var MM = p2(d.getMonth() + 1);
var DD = p2(d.getDate());
var hh = p2(d.getHours());
var mm = p2(d.getMinutes());
var ss = p2(d.getSeconds());
var sss = d.getMilliseconds().toString().padStart(3, "0");
var offset = formatOffset(-d.getTimezoneOffset());
return `${YYYY}-${MM}-${DD}T${hh}:${mm}:${ss}.${sss}${offset}`;
}
function formatOffset(minutes) {
if (!minutes) {
return "Z";
}
var h = Math.floor(Math.abs(minutes) / 60);
var m = Math.abs(minutes) % 60;
var offset = "";
if (minutes > 0) {
offset = "+";
} else if (minutes < 0) {
offset = "-";
}
// +0500, -0730
return offset + p2(h) + p2(m);
}
function p2(x) {
return String(x).padStart(2, "0");
}
exports.toLocalISOString = toLocalISOString;
})(("undefined" === typeof module && window) || exports);

28
xtz.js
View File

@ -98,9 +98,7 @@ var XTZ;
} }
// +0500, -0730 // +0500, -0730
return ( return offset + p2(h) + p2(m);
offset + h.toString().padStart(2, "0") + m.toString().padStart(2, "0")
);
} }
function toOffsetISOString(d) { function toOffsetISOString(d) {
@ -155,6 +153,27 @@ var XTZ;
return toOffsetISOString(whole); return toOffsetISOString(whole);
} }
function toLocalISOString(dateOrStr) {
var d;
if (dateOrStr) {
d = new Date(dateOrStr);
} else {
d = new Date();
}
var YYYY = d.getFullYear();
var MM = p2(d.getMonth() + 1);
var DD = p2(d.getDate());
var hh = p2(d.getHours());
var mm = p2(d.getMinutes());
var ss = p2(d.getSeconds());
var sss = d.getMilliseconds().toString().padStart(3, "0");
var offset = formatOffset(-d.getTimezoneOffset());
return `${YYYY}-${MM}-${DD}T${hh}:${mm}:${ss}.${sss}${offset}`;
}
XTZ = { XTZ = {
// bespoke date => // bespoke date =>
// 2021-11-07T3:15:59-0500 // 2021-11-07T3:15:59-0500
@ -163,6 +182,9 @@ var XTZ;
// -240 => -0400 // -240 => -0400
formatOffset: formatOffset, formatOffset: formatOffset,
// new Date() => "2021-11-07T03:15:59-0500"
toLocalISOString: toLocalISOString,
// [ "2021-11-07T08:15:59Z", "America/New_York" ] // [ "2021-11-07T08:15:59Z", "America/New_York" ]
// => "2021-11-07T03:15:59-0500" // 2021-11-07 03:15:59 // => "2021-11-07T03:15:59-0500" // 2021-11-07 03:15:59
toTimeZone: toTimeZone, toTimeZone: toTimeZone,