@swim/time provides date-time, time zone, and time interval data types,
with strptime/strftime-style parsers and formatters. @swim/time
facilitates parsing and formatting of date strings, time zone aware date
manipulation, and sampling of date ranges at regular time intervals.
@swim/time is part of the @swim/core
framework.
The DateTime class models an immutable instant in time, relative to a
particular TimeZone. The DateTime.current static method returns the
current time in the local time zone, or in an optionally specified time zone.
The DateTime.fromInit static method coerces plain JavaScript objects, of type
DateTimeInit, to instances of DateTime. DateTime.fromInit defaults to UTC,
but can optionally be passed a specific time zone.
The DateTime.fromAny static method coerces common JavaScript date
representations, including ECMAScript Date objects, numbers representing
milliseconds since the Unix epoch, and ISO 8601-formatted date-time strings,
to DateTime instances.
The TimeZone class represents an immutable offset, in minutes, from Universal
Coordinated Time (UTC). The TimeZone.local and TimeZone.utc static methods
return the current local time zone, and the UTC time zone, respectively.
A DateTimeFormat represents a string encoding that parse date-time strings
as DateTime objects, and format DateTime objects as date-time strings.
The DateTimeFormat.iso8601 static method returns the standard ISO 8601
date-time format. The DateTimeFormat.pattern method returns a
DateTimeFormat that parses and formats date-times according to a
strptime/strftime-style format string.
Use the parse method of a DateTimeFormat to parse a DateTime object from
a compatible date-time string:
Use the format method of a DateTimeFormat to serialize a DateTime object
to a compatible date-time string. You can also optionally pass a
DateTimeFormat to a DateTime's toString method.
A DateTimeLocale specifies the period, weekday, short weekday, month, and
short month strings used when parsing and formatting date-time strings.
DateTimeLocale.standard() returns the standard English language locale.
A TimeInterval represents a regular duration of time. A UnitTimeInterval
represents a time interval with a uniform duration. Milliseconds, seconds,
minutes, hours, and days are unit time intervals. Weeks, months, and years
are not unit time intervals, because different weeks, months, and years can
have different durations.
Time intervals can be created with the TimeInterval.millisecond,
TimeInterval.second, TimeInterval.minute, TimeInterval.hour,
TimeInterval.day, TimeInterval.week, TimeInterval.month, and
TimeInterval.year factory methods.
A TimeInterval can be used to offset a DateTime by a multiple of the
interval, to advance to the next whole multiple of the interval, to round
a DateTime down to the floor of the interval, to round a DateTime up
to the ceil of the interval, or to round a DateTime to the nearest
whole interval.
The range method of a TimeInterval returns an array of DateTimes
representing every whole interval between some start time (inclusive), and
some end time (exclusive). An optional third argument to range indicates
that only every step multiple of the base interval should be included in
the returned range.
The TimeInterval.milliseconds, TimeInterval.seconds, TimeInterval.minutes,
TimeInterval.hours, TimeInterval.days, TimeInterval.weeks,
TimeInterval.months, and TimeInterval.years factory methods provide
a shorthand for computing a range of DateTimes between two times, and
return the equivalent of calling range on the underlying time interval.
For an npm-managed project, npm install @swim/time to make it a dependency.
TypeScript sources will be installed into node_modules/@swim/time/main.
Transpiled JavaScript and TypeScript definition files install into
node_modules/@swim/time/lib/main. And a pre-built UMD script can
be found in node_modules/@swim/time/dist/main/swim-time.js.
Browser applications can load swim-core.js, which comes bundled with the
@swim/time library, directly from the SwimOS CDN.
<!-- Development --><scriptsrc="https://cdn.swimos.org/js/latest/swim-core.js"></script><!-- Production --><scriptsrc="https://cdn.swimos.org/js/latest/swim-core.min.js"></script>
Alternatively, the standalone swim-system.js script may be loaded
from the SwimOS CDN, which bundles @swim/time together with all other
@swim/system
libraries.
<!-- Development --><scriptsrc="https://cdn.swimos.org/js/latest/swim-system.js"></script><!-- Production --><scriptsrc="https://cdn.swimos.org/js/latest/swim-system.min.js"></script>
When loaded by a web browser, the swim-core.js script adds all
@swim/time library exports to the global swim namespace.
The swim-system.js script also adds all @swim/time library exports
to the global swim namespace, making it a drop-in replacement for
swim-core.js when additional @swim/system libraries are needed.
@swim/time
@swim/time provides date-time, time zone, and time interval data types, with
strptime
/strftime
-style parsers and formatters. @swim/time facilitates parsing and formatting of date strings, time zone aware date manipulation, and sampling of date ranges at regular time intervals. @swim/time is part of the @swim/core framework.Overview
DateTime
The
DateTime
class models an immutable instant in time, relative to a particularTimeZone
. TheDateTime.current
static method returns the current time in the local time zone, or in an optionally specified time zone.The
DateTime.fromInit
static method coerces plain JavaScript objects, of typeDateTimeInit
, to instances ofDateTime
.DateTime.fromInit
defaults to UTC, but can optionally be passed a specific time zone.The
DateTime.fromAny
static method coerces common JavaScript date representations, including ECMAScriptDate
objects, numbers representing milliseconds since the Unix epoch, and ISO 8601-formatted date-time strings, toDateTime
instances.TimeZone
The
TimeZone
class represents an immutable offset, in minutes, from Universal Coordinated Time (UTC). TheTimeZone.local
andTimeZone.utc
static methods return the current local time zone, and the UTC time zone, respectively.DateTimeFormat
A
DateTimeFormat
represents a string encoding that parse date-time strings asDateTime
objects, and formatDateTime
objects as date-time strings. TheDateTimeFormat.iso8601
static method returns the standard ISO 8601 date-time format. TheDateTimeFormat.pattern
method returns aDateTimeFormat
that parses and formats date-times according to astrptime
/strftime
-style format string.Use the
parse
method of aDateTimeFormat
to parse aDateTime
object from a compatible date-time string:Use the
format
method of aDateTimeFormat
to serialize aDateTime
object to a compatible date-time string. You can also optionally pass aDateTimeFormat
to aDateTime
'stoString
method.DateTimeLocale
A
DateTimeLocale
specifies the period, weekday, short weekday, month, and short month strings used when parsing and formatting date-time strings.DateTimeLocale.standard()
returns the standard English language locale.TimeInterval
A
TimeInterval
represents a regular duration of time. AUnitTimeInterval
represents a time interval with a uniform duration. Milliseconds, seconds, minutes, hours, and days are unit time intervals. Weeks, months, and years are not unit time intervals, because different weeks, months, and years can have different durations.Time intervals can be created with the
TimeInterval.millisecond
,TimeInterval.second
,TimeInterval.minute
,TimeInterval.hour
,TimeInterval.day
,TimeInterval.week
,TimeInterval.month
, andTimeInterval.year
factory methods.A
TimeInterval
can be used tooffset
aDateTime
by a multiple of the interval, to advance to thenext
whole multiple of the interval, to round aDateTime
down to thefloor
of the interval, to round aDateTime
up to theceil
of the interval, or toround
aDateTime
to the nearest whole interval.The
every
method of aUnitTimeInterval
returns a newTimeInterval
equal to a multiple of the base time interval.The
range
method of aTimeInterval
returns an array ofDateTime
s representing every whole interval between some start time (inclusive), and some end time (exclusive). An optional third argument torange
indicates that only everystep
multiple of the base interval should be included in the returned range.The
TimeInterval.milliseconds
,TimeInterval.seconds
,TimeInterval.minutes
,TimeInterval.hours
,TimeInterval.days
,TimeInterval.weeks
,TimeInterval.months
, andTimeInterval.years
factory methods provide a shorthand for computing a range ofDateTime
s between two times, and return the equivalent of callingrange
on the underlying time interval.Installation
npm
For an npm-managed project,
npm install @swim/time
to make it a dependency. TypeScript sources will be installed intonode_modules/@swim/time/main
. Transpiled JavaScript and TypeScript definition files install intonode_modules/@swim/time/lib/main
. And a pre-built UMD script can be found innode_modules/@swim/time/dist/main/swim-time.js
.Browser
Browser applications can load
swim-core.js
, which comes bundled with the @swim/time library, directly from the SwimOS CDN.Alternatively, the standalone
swim-system.js
script may be loaded from the SwimOS CDN, which bundles @swim/time together with all other @swim/system libraries.Usage
ES6/TypeScript
@swim/time can be imported as an ES6 module from TypeScript and other ES6-compatible environments.
CommonJS/Node.js
@swim/time can also be used as a CommonJS module in Node.js applications.
Browser
When loaded by a web browser, the
swim-core.js
script adds all @swim/time library exports to the globalswim
namespace.The
swim-system.js
script also adds all @swim/time library exports to the globalswim
namespace, making it a drop-in replacement forswim-core.js
when additional @swim/system libraries are needed.