Isn't it weird how we can do math in our head, but not date math?
- how many days until the end of the year?
-what time was it, 11 hours ago?
-is it lunchtime in france?
and worse - there is no real date calculator.
people end up asking google, and going to weird websites.
that's bad.
spacetime is a date-calculator,
It's very small, and very handy.
let s = spacetime.now()
s.diff(s.endOf('year'), 'days')
s.substract(11, 'hours').time()
s = s.goto('Europe/Paris')
s.isAfter(s.time('11:00am'))
- calculate time in remote timezones
- support daylight savings, leap years, and hemispheres
- Moment-like API (but immutable)
- Orient time by quarter, season, month, week..
- Zero Dependencies - (no Intl API)
- weighs about 40kb.
- has a cool plugin thing.
<script src="https://unpkg.com/spacetime"></script>
<script>
var d = spacetime('March 1 2012', 'America/New_York')
d = d.time('4:20pm')
d = d.goto('America/Los_Angeles')
d.time()
</script>
npm install spacetime
const spacetime = require('spacetime')
let d = spacetime.now('Europe/Paris')
d.dayName()
d.isAsleep()
typescript / babel / deno:
import spacetime from 'spacetime'
let d = spacetime.now()
d.format('nice')
plugins:
Date Inputs:
we can parse all the normal stuff, and some fancy stuff:
s = spacetime(1489520157124)
s = spacetime([2017, 5, 2])
s = spacetime('July 2, 2017 5:01:00')
s = spacetime(1489520157124, 'Canada/Pacific')
s = spacetime('2019/05/15', 'Canada/Pacific')
s = spacetime('2017-04-03T08:00:00-0700')
s = spacetime.now()
s = spacetime.today()
s = spacetime.tomorrow()
s = spacetime.min()
s = spacetime.max()
jsDate = spacetimeDate.toNativeDate()
for fancier natural-language inputs, use compromise-dates.
Get & Set dates:
you can whip things around, but stay intuitive
s.date()
s.year()
s.season()
s = s.hour(5)
s = s.date(15)
s = s.day('monday')
s = s.day('monday', true)
s = s.day('monday', false)
s = s.month('march')
s = s.quarter(2)
s.era()
s.decade()
s.century()
s.progress().month = 0.23
s.progress().day = 0.48
s.progress().hour = 0.99
s = s.add(1, 'week')
s = s.add(3, 'quarters')
s = s.subtract(2, 'months').add(1, 'day')
s = s.startOf('day')
s = s.startOf('month')
s = s.endOf('quarter')
s = s.nearest('hour')
s = s.nearest('quarter-hour')
s = s.next('month')
s = s.last('year')
s.every('week', 'Jan 1st 2020')
s.clone()
s.isValid()
s.isAwake()
s.json()
if it's 9am on tuesday, and you add a week, it will still be 9am on tuesday.
... even if some crazy changes happen.
setter methods also support a handy 2nd param that controls whether it should be set forward, or backward.
s = s.time('4:00pm')
s = s.time('4:00pm', true)
s = s.time('4:00pm', false)
s = s.set('march 5th 2020')
s = s.set('march 4th')
s = s.set('march 4th', true)
s = s.set('march 6th', false)
it's actually a little surprising how helpful this is.
Comparisons:
let s = spacetime([2017, 5, 2])
let start = s.subtract(1, 'milliseconds')
let end = s.add(1, 'milliseconds')
s.isAfter(d)
s.isEqual(d)
s.isBefore(d)
s.isBetween(start, end, inclusive?)
s.isSame(d, 'year')
s.isSame(d, 'date')
s.diff(d, 'day')
s.diff(d, 'month')
let before = spacetime([2018, 3, 28])
let now = spacetime([2017, 3, 28])
now.since(before)
all comparisons are done with sensitivity of timezone - 8am EST is < 8am PST.
Timezones:
the best way to describe a timezone is an IANA code:
s = s.goto('Australia/Brisbane')
if you want to support relaxed timezone names like 'EST'
, Eastern time
, use timezone-soft
spacetime.extend(require('timezone-soft'))
s = s.goto('milwaukee')
s = s.goto('-7h')
s = s.goto('GMT+8')
play-around with timezones, and their DST-changes:
spacetime.whereIts('8:30pm', '9:30pm')
spacetime.whereIts('9am')
s.timezone().name
s.hemisphere()
s.timezone().current.offset
s.hasDST()
s.isDST()
spacetime.timezones()
you can flip-around the world pretty quick.
spacetime will use your local timezone, by default:
.goto(null)
will pluck your current tz safely from your browser or computer.
spacetime().time('4:30pm').goto('Europe/Paris').goto(null).time()
Date Formatting:
it's a pretty-sensible process to create nice-looking dates:
s.format('time')
s.format('numeric-uk')
s.format('month')
s.format('month-short')
s.format('month-pad')
s.format('iso-month')
s.format('{year}-{date-pad}-{month-pad}')
s.format("{hour} o'clock")
s.format('{time}{ampm} sharp')
s.unixFmt('yyyy.MM.dd h:mm a')
Limitations & caveats
◆ Historical timezone info
DST changes move around all the time, and timezones pop-in and out of existence.
We store and use only the latest DST information, and apply it to historical dates.
◆ International date line
.goto()
never crosses the date-line. This is mostly the intuitive behaviour.
But if you're in Fiji
(just west of the date line), and you go to Midway
(just east of the date line), .goto() will subtract a bunch of hours, instead of just adding one.
◆ Destructive changes
if it's 2:30pm
and you add a month, it should still be 2:30pm
. Some changes are more destructive than others. Many of thse choices are subjective, but also sensible.
◆ 0-based vs 1-based ...
for better or worse we copy the JavaScript spec for 0-based months, and 1-based dates.
ISO-formatting is different, so keep on your toes.
see more considerations and gotchas
Daylight-savings gotchas
We've written in detail about how spacetime handles Daylight-savings changes here
Fall DST changes have an hour that is repeated twice. There are a lot of tricky situations that come from this.
Add 10 minutes at 1:55am
, and a spacetime diff may show -50mins
. Within an hour of this change, some spacetime methods may be off-by-one hour.
Springtime DST changes are generally smoother than Fall ones.
Config:
Ambiguity warnings:
javascript dates use millisecond-epochs, instead of second-epochs, like some other languages.
This is a common bug, and spacetime can warn if you set an epoch within January 1970.
to enable:
let s = spacetime(123456, 'UTC', {
silent: false
})
s.log()
There is another situation where you may see a console.warn
- if you give it a timezone, but then set a ISO-date string with a different offset, like 2017-04-03T08:00:00-0700
(-7hrs UTC offset).
It sets the timezone to UTC-7, but also gives a warning.
let s = spacetime('2017-04-03T08:00:00-0700', 'Canada/Eastern', {
silent: false
})
s.timezone().name
Configure 'today' context:
spacetime makes some assumptions about some string inputs:
let s = spacetime('June 1992')
s.date()
let s = spacetime('June 5th')
s.year()
let s = spacetime('2030')
s.month()
you can configure this assumed date (usually for testing) by passing it in as an option:
let today = {
month: 3,
date: 4,
year: 1996
}
let s = spacetime('June 5th', null, { today: today })
s.year()
it also works for spacetime.now(tz, {today:today})
and others.
Extending/Plugins:
you can throw any methods onto the Spacetime class you want, with spacetime.extend()
:
spacetime.extend({
isHappyHour: function () {
return this.hour() === 16
}
})
let s = spacetime.now('Australia/Adelaide')
s.isHappyHour()
s = s.time('4:30pm')
s.isHappyHour()
DD/MM/YYY interpretation:
by default spacetime uses the American interpretation of ambiguous date formats, like javascript does:
spacetime('12/01/2018')
spacetime('13/01/2018')
you can change this behaviour by passing in a dmy
option, like this:
spacetime('12/01/2018', null, { dmy: true })
this format is more common in britain, and south america.
Custom languages:
let s = spacetime.now()
s.i18n({
days: {
long: ['domingo', 'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado'],
short: ['dom', 'lun', 'mar', 'mié', 'jue', 'vie', 'sáb']
},
months: {
long: [...],
short: ['ene', 'feb', 'mar', 'abr', 'may', 'jun', 'jul', 'ago', 'sep', 'oct', 'nov', 'dic'],
},
ampm: {
am: ' a. m.',
pm: ' a. m.'
},
distance: {
past: 'past',
future: 'future',
present: 'present',
now: 'now',
almost: 'almost',
over: 'over',
pastDistance: (value) => `${value} ago`,
futureDistance: (value) => `in ${value}`
},
units: {
second: 'second',
secondPlural: 'second',
minute: 'minute',
minutePlural: 'minutes',
hour: 'hour',
hourPlural: 'hours',
day: 'day',
dayPlural: 'seconds',
month: 'month',
monthPlural: 'months',
year: 'year',
yearPlural: 'years',
},
useTitleCase: true
});
s.format('day')
Configure start of week:
by default, the start of the week is monday.
You can determine the week by the official country setting, with spacetime-week
let s = spacetime.now()
s = s.weekStart('sunday')
s = s.startOf('week')
s.dayName()
s = s.endOf('week')
s.dayName()
See also:
thank you to the amazing timeanddate.com
Apache 2.0