year
month
week
day
hour
minute
API:
main methods
compare, format, and move around
.goto() → clonemove to a new timezone, but at this same moment. Accepts an IANA code or abbreviation.clone() → clonemake a copy of this object, with no references to the original.timezone() → Object/clonereturn a bunch of meta-data about your current timezone, or pass a name to swap the timezone while keeping the same date-time.toNativeDate() → Datereturn the native javascript Date object at the same epoch (falls back to the local timezone).format() → String/Objectoutput nicely-formatted strings, using a named format or a {token} template.unixFmt() → Stringoutput a formatted string using unix/moment formatting tokens, like 'yyyy.MM.dd h:mm a'.startOf() → clonemove to the first millisecond of the day, week, month, year, etc..endOf() → clonemove to the last millisecond of the day, week, month, year, etc..add() → cloneincrement the time by a number and unit - like an hour, minute, day, or year.subtract() → clonedecrease the time by a number and unit - like an hour, minute, day, or year.next() → clonego to the beginning of the next unit.last() → clonego to the beginning of the previous unit.nearest() → clonemove forward/backward to the closest unit.round() → cloneround to either the current, or +1 of this unit (alias of nearest).every() → Arraylist all dates up to a certain time, by a given unit.each() → Arraylist all dates between this one and another, by a given unit.isAfter() → Booleanpass-in a spacetime object or date input and see if it takes-place after your spacetime date/time.isBefore() → Booleanpass-in a spacetime object or date input and see if it takes-place before your spacetime date/time.isEqual() → Booleanis this date on the exact same millisecond as another.isBetween() → Booleanis this date between these start and end dates? accepts an optional isInclusive flag.isSame() → Booleandetect if two date/times are the same day, week, or year, etc.diff() → Number/Objectgiven a date and a unit, count how many of them you'd need to make the dates equal. Without a unit, returns a count of every unit..since() → Objectcreate a human-readable diff between the two dates, like 'in 5 months'.from() → Objectcreate a human-readable diff between the two dates (alias of since).fromNow() → Objectcreate a human-readable diff between now and the given dateget + set
call with no params to get, pass a value to set
.millisecond() → clone/Numberset or return the current number of milliseconds (0-999).second() → clone/Numberset or return the current number of seconds (0-59).minute() → clone/Numberset or return the current number of minutes (0-59).hour() → clone/Numberset or return the current hour, in 24 time (0-23). also accepts/parses '3pm'.hour12() → clone/Numberset or return the current hour, in 12-hour format (0-11). also accepts/parses '3pm'.date() → clone/Numberset or return the day-number of the month (1- max31).month() → clone/Numberset or return the zero-based month-number (0-11). Also accepts 'June', or 'oct'..year() → clone/Numberset or return the 4-digit year as an integer.decade() → clone/Numberset or return the current decade, like 2020.century() → clone/Numberset or return the current century, like 21.millennium() → clone/Numberset or return the current millennium, like 3.dayOfYear() → clone/Numberset or return the day of the year (1-366). Jan 1st is 1, Dec 31st is 366..time() → clone/Stringset or return a formatted, 12-hour time, like '11:30pm'.week() → clone/Numberset or return the week-number of the year (1-52)..quarter() → clone/Numberset or return the fiscal-quarter (1-4).season() → clone/Stringset or return the name of the season, spring/summer/fall/autumn/winter.era() → clone/Stringset or return the era, 'BC' or 'AD'.hourFloat() → clone/Numberset or return the hour + minute in decimal form, so '3:30am' is 3.5.day() → clone/Numberset or return the day of the week as an integer, starting on sunday (day-0). Also accepts names like 'wednesday', or 'thurs'.dayName() → clone/Stringset or return the day of the week as a string, like 'wednesday'.ampm() → clone/Stringset or return whether the time is am or pm.dayTime() → clone/Stringset or return the general time-of-day, like 'afternoon'.monthName() → clone/Stringset or return the current month as a string, like 'april'.epochSeconds() → clone/Numberset or return the time in seconds since jan 1 1970.iso() → clone/Stringset or return the traditional ISO 8601 datetime string.isoFull() → clone/Stringset or return the extended ISO datetime string (RFC 9557), with offset and [timezone].json() → clone/Objectset or return all date units as a key-value objectutilities
inspection and configuration helpers
.set() → clonechange to a new date..isValid() → Booleandoes this time exist on the gregorian/javascript calendar?.log() → clonepretty-print the date to the console, for nicer debugging.logYear() → clonepretty-print the full date to the console, for nicer debugging.progress() → ObjectBetween 0-1, how far the moment lands between the start and end of the day/week/month/year..leapYear() → Booleanis the current year a leap year?.isDST() → Booleanis daylight-savings-time activated right now, for this timezone?.inDST() → Booleanis daylight-savings-time activated right now, for this timezone? (alias of isDST).hasDST() → Booleandoes this timezone ever use daylight-savings.offset() → Numberthe current, DST-aware time-difference from UTC, in hours.hemisphere() → Stringwhich hemisphere is this timezone in, 'North' or 'South'.isAwake() → Booleanchecks if the current time is between 8am and 10pm.isAsleep() → Booleanchecks if the current time is between 10pm and 8am.i18n() → clonechanges the names of months and days, for non-english formatting.weekStart() → clonechange the day the week starts on (0-6, default is monday).daysInMonth() → Numberreturns the amount of days the current month has (December => 31, June => 30, ...)static methods
on the spacetime object itself
spacetime.now() → clonecreate a spacetime object at the current moment. Accepts a timezone as the first argument.spacetime.today() → clonecreate a spacetime object at the start of today (this morning)spacetime.tomorrow() → clonecreate a spacetime object at the start of tomorrowspacetime.yesterday() → clonecreate a spacetime object at the start of yesterdayspacetime.min() → clonecreate a spacetime object at the earliest-possible date (271,821 bc)spacetime.max() → clonecreate a spacetime object at the furthest-possible future datespacetime.fromUnixSeconds() → clonecreate a spacetime object from an epoch in seconds (instead of milliseconds)spacetime.extend() → spacetimeadd custom methods (or a plugin) to the spacetime objectspacetime.timezones() → Objectget a list of all known timezones and their offsetsspacetime.whereIts() → Arraylist the timezones where it's currently a given time, like '8:30pm' full docs at github.com/spencermountain/spacetime