Web Development

Lightweight Way to Handle Timezones in JS – Spacetime

Riz Ali
Riz
Aug 27, 2020
2 min read
350 words

A lightweight way to manipulate, traverse, compare, and format dates and times across planet Earth

Spacetime does some ugly things. it can sometimes get confused. but by making realistic compromises space-time considerably under-weighs most date libraries.
It lets you have a mostly-great time, without thinking about awful things.

  1. Get/set dates and times in remote timezone
  2. Global support for Daylight Savings Time, leap years + seconds, and hemispheres
  3. Orient by quarter, season, month, and week
  4. Remote date comparison
  5. Written in ES2015 JS, published as ES5, tested for Node and the browser
  6. Weighs in at just 35KB (12KB compressed)
  7. Well tested, Apache 2.0 licensed
  8. Made by your friendly friends at Begin

Installation

<script src="https://unpkg.com/spacetime"></script>
<script>
  var d = spacetime('March 1 2012', 'America/New_York')
  //set the time
  d = d.time('4:20pm')

  d = d.goto('America/Los_Angeles')
  d.time()
  //'1:20pm'
</script>

npm install spacetime

const spacetime = require('spacetime')
let d = spacetime.now('Europe/Paris')
d.dayName()
//'Wednesday'
d.isAsleep()
//true

Or with Typescript

import spacetime from 'spacetime'
let d = spacetime.now()
d.format('nice')
//'Apr 1st, 4:32pm'

make sure to add this to your tsconfig.json:

{
  "compilerOptions": {
    "esModuleInterop": true
  }
}

changelog

v6.6.2
[fix] for formatting when the output is 0 ????
[fix] for leap-day in .date() method
update deps
add github release/publish script
v5.9.0
add support for changing start/end of the week
allow more flexible .i18n() inputs
v5.8.0
faster diff for large-number of months
more support for esoteric iso formats
add format('iso-month')
change fallback behaviour for set failure edge-cases
v5.7.0
avoid setting dates/months/hours past maximums and minimums
.hour(24) now changes date to tomorrow
faster diffs for large (decade-long) ranges
[big] fix awkward diff/since issues ('dec 25->jan 5' should be 0 years)
v5.6.0
support typescript types by Jacob Craig
use given timezone when given a spacetime object as an input
adds .every(unit, to) method
set UTC as default fallback timezone
V5.4.0
support more time input formats
fixes offset input issue #103
V5.3.0
added more iana timezones
fixed zero-padding in iso offsets
support half hour offsets in iso formats
Drag and Drop Mini AJAX File Upload Form
Riz Ali

Riz

Author

Riz is a Senior Web Strategist with over 14+ years of hands-on experience in full-stack web design and digital architecture. Having successfully delivered 250+ web projects, he believes passion is the engine of innovation. Riz specializes in translating complex technical concepts into accessible, fanatical content, helping businesses bridge the gap between aesthetic design and high-performance engineering. His work has been instrumental in scaling digital presence for startups and established brands alike.

View all posts

Continue reading

You might also like

Leave a Reply

Your email address will not be published. Required fields are marked *

Menu