The idea for Temporal came from a conversation about how poor the Date implementation of JavaScript is. Some history of that is in this blogpost, where Brendan Eich reveals he pretty much copied Java date handling given his short timescale to get JavaScript implemented.
At the time of writing, Temporal is still Stage 3 proposal for addition to the ECMAScript language, but that’s pretty far along so we can expect it to see it implemented as a JavaScript API in the not too distant future. There is a working polyfill available for Node here, but I’ve created a library from a rollup of it and its dependencies so we can start to use it from Apps Script right now.
Since it’s only a proposal, the details may change, but I’ll try to keep it up to date as we go along. It’s a pretty sizeable polyfill, and I didn’t minify it on purpose for now, in case you want to have a look into the details of how it works. I’ll probably do that in a later update.
Apps Script usage
Include bmTemporal in your project
"userSymbol": "bmTemporal", "libraryId": "1o_qJw1fdgF4NgRnHrsB_Jsbu3gYe1fQnPze33V9jHLqBCXHmzZaBgmGH"
const {Temporal} = bmTemporal
Fundamentals
The major problems that Temporal is designed to solve (Moment.js already helps solve many of these – but Temporal will be built into JavaScript in due course)
- date mutability
- date artithmetic and comparision
- parser unreliability
- timezone spaghetti
- alternative calendars
- millisecond accuracy
Another issue that people get worried about is if there’s a ‘Y2k’ problem for JavaScript date. Well there might be, but not until
Sat Sep 13 275760
So I don’t imagine JavaScript will be around then (even though VBA might be!)
Examples
Concepts
Exact time
Temporal.Instant
UTC now
const timeStamp = Temporal.now.instant()
equivalent to
new Date()
Get milliseconds
const ms = Temporal.now.instant().epochMilliseconds
equivalent to
new Date().getTime()
Temporal.ZonedDateTime
Wall-clock time
Plain dates
Temporal.PlainDate
There are also variants for
Temporal.PlainTime
and
Temporal.PlainDateTime
Note that when you pass a date string for parsing that actually contains a TimeZone it will be used to calculate the PlainDate, otherwise the local timezone will be used.
The local date/time now
const local = Temporal.plainDateTimeISO()
TimeZones
The IANA or tz database is a list of known timezones names.
An entry contains things like the DST rules, the timezone offset from UTC and various other rules an info. Temporal uses either the name of the offset to reference a timezone. A datestring containing a timezone name looks like this
2019-09-03T17:34:05+09:00[Asia/Tokyo]
and one containing an offset looks like this
2020-09-06T10:35:24.485-07:00
Temporal.TimeZone
A TimeZone can be created futlike this
const tz = Temporal.TimeZone.from(‘Europe/London’);
When dealing with Spreadsheets, you may want to get the timezone of the sheet
const tz = Temporal.TimeZone.from(SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone())
console.log(Temporal.now.instant().toString({timeZone:tz}))
Temporal Arithmetic
Links
bmTemporal: IDE
libraryId: 1o_qJw1fdgF4NgRnHrsB_Jsbu3gYe1fQnPze33V9jHLqBCXHmzZaBgmGH