d3-time-format
This module provides a JavaScript implementation of the venerable strptime and strftime functions from the C standard library, and can be used to parse or format dates in a variety of locale-specific representations. To format a date, create a format function from a format specifier (a string with the desired format directives, indicated by %
); then pass a date to the format function, which returns a string. For example, to convert the current date to a human-readable string:
var f = d3_time_format.format("%B %d, %Y");
f(new Date);
Format functions also support parsing as format.parse, so to convert a string back to a date:
var f = d3_time_format.format("%B %d, %Y");
f.parse("June 30, 2015");
You can implement more elaborate conditional time formats, too. For example, here’s a multi-scale time format using time intervals:
var formatMillisecond = d3_time_format.format(".%L"),
formatSecond = d3_time_format.format(":%S"),
formatMinute = d3_time_format.format("%I:%M"),
formatHour = d3_time_format.format("%I %p"),
formatDay = d3_time_format.format("%a %d"),
formatWeek = d3_time_format.format("%b %d"),
formatMonth = d3_time_format.format("%B"),
formatYear = d3_time_format.format("%Y");
function multiFormat(date) {
return (d3_time.second(date) < date ? formatMillisecond
: d3_time.minute(date) < date ? formatSecond
: d3_time.hour(date) < date ? formatMinute
: d3_time.day(date) < date ? formatHour
: d3_time.month(date) < date ? (d3_time.week(date) < date ? formatDay : formatWeek)
: d3_time.year(date) < date ? formatMonth
: formatYear)(date);
}
This module is used by D3 time scales to generate human-readable ticks.
Installing
If you use NPM, npm install d3-time-format
. Otherwise, download the latest release. The released bundle supports AMD, CommonJS, and vanilla environments. Create a custom build using Rollup or your preferred bundler. You can also load directly from d3js.org:
<script src="https://d3js.org/d3-time.v0.1.min.js"></script>
<script src="https://d3js.org/d3-time-format.v0.2.min.js"></script>
In a vanilla environment, a d3_time_format
global is exported. Try d3-time-format in your browser.
API Reference
# d3_time_format.format(specifier)
An alias for locale.format on the U.S. English locale. See the other locales, or use locale to define a new locale.
# d3_time_format.utcFormat(specifier)
An alias for locale.utcFormat on the U.S. English locale. See the other locales, or use locale to define a new locale.
# d3_time_format.isoFormat
The full ISO 8601 UTC time format function. Where available, this method will use Date.toISOString to format and the Date constructor to parse strings. If you depend on strict validation of the input format according to ISO 8601, you should construct a UTC format:
var isoFormat = d3_time_format.utcFormat("%Y-%m-%dT%H:%M:%S.%LZ");
# locale.format(specifier)
Returns a new format function for the given string specifier. The specifier string may contain the following directives:
%a
- abbreviated weekday name.*%A
- full weekday name.*%b
- abbreviated month name.*%B
- full month name.*%c
- the locale’s date and time, such as %a %b %e %H:%M:%S %Y
.*%d
- zero-padded day of the month as a decimal number [01,31].%e
- space-padded day of the month as a decimal number [ 1,31]; equivalent to %_d
.%H
- hour (24-hour clock) as a decimal number [00,23].%I
- hour (12-hour clock) as a decimal number [01,12].%j
- day of the year as a decimal number [001,366].%m
- month as a decimal number [01,12].%M
- minute as a decimal number [00,59].%L
- milliseconds as a decimal number [000, 999].%p
- either AM or PM.*%S
- second as a decimal number [00,61].%U
- Sunday-based week of the year as a decimal number [00,53].%w
- Sunday-based weekday as a decimal number [0,6].%W
- Monday-based week of the year as a decimal number [00,53].%x
- the locale’s date, such as %m/%d/%Y
.*%X
- the locale’s time, such as %H:%M:%S
.*%y
- year without century as a decimal number [00,99].%Y
- year with century as a decimal number.%Z
- time zone offset, such as -0700
, -07:00
, -07
, or Z
.%%
- a literal percent sign (%
).
Directives marked with an asterisk (*) may be affected by the locale definition. For %U
, all days in a new year preceding the first Sunday are considered to be in week 0. For %W
, all days in a new year preceding the first Monday are considered to be in week 0. Week numbers are computed using interval.count.
The %
sign indicating a directive may be immediately followed by a padding modifier:
0
- zero-padding_
- space-padding-
- disable padding
If no padding modifier is specified, the default is 0
for all directives except %e
, which defaults to _
. (In some implementations of strftime and strptime, a directive may include an optional field width or precision; this feature is not yet implemented.)
# locale.utcFormat(specifier)
Equivalent to locale.format, except all directives are interpreted as Coordinated Universal Time (UTC) rather than local time.
# format(date)
Formats the specified date, returning the corresponding string.
var formatMonth = d3_time_format.format("%B"),
formatDay = d3_time_format.format("%A"),
date = new Date(2014, 4, 1);
formatMonth(date);
formatDay(date);
# format.parse(string)
Parses the specified string, returning the corresponding date or null if the string could not be parsed according to this format’s specifier.
Parsing is strict: if the specified string does not exactly match the associated specifier, this method returns null. For example, if the associated specifier is %Y-%m-%dT%H:%M:%SZ
, then the string "2011-07-01T19:15:28Z"
will be parsed as expected, but "2011-07-01T19:15:28"
, "2011-07-01 19:15:28"
and "2011-07-01"
will return null. (Note that the literal Z
here is different from the time zone offset directive %Z
.) If a more flexible parser is desired, try multiple formats sequentially until one returns non-null.
The %d
and %e
directives are considered equivalent for parsing.
# format.toString()
Returns this format’s specifier.
Locales
# d3_time_format.locale(definition)
Returns a locale object for the specified definition with locale.format and locale.utcFormat methods. The definition must include the following properties:
dateTime
- the date and time (%c
) format specifier (e.g., "%a %b %e %X %Y"
).date
- the date (%x
) format specifier (e.g., "%m/%d/%Y"
).time
- the time (%X
) format specifier (e.g., "%H:%M:%S"
).periods
- the A.M. and P.M. equivalents (e.g., ["AM", "PM"]
).days
- the full names of the weekdays, starting with Sunday.shortDays
- the abbreviated names of the weekdays, starting with Sunday.months
- the full names of the months (starting with January).shortMonths
- the abbreviated names of the months (starting with January).
# d3_time_format.localeCaEs
Catalan (Spain)
# d3_time_format.localeDeCh
German (Switzerland)
# d3_time_format.localeDeDe
German (Germany)
# d3_time_format.localeEnCa
English (Canada)
# d3_time_format.localeEnGb
English (United Kingdom)
# d3_time_format.localeEnUs
English (United States)
# d3_time_format.localeEsEs
Spanish (Spain)
# d3_time_format.localeFiFi
Finnish (Finland)
# d3_time_format.localeFrCa
French (Canada)
# d3_time_format.localeFrFr
French (France)
# d3_time_format.localeHeIl
Hebrew (Israel)
# d3_time_format.localeHuHu
Hungarian (Hungary)
# d3_time_format.localeItIt
Italian (Italy)
# d3_time_format.localeJaJp
Japanese (Japan)
# d3_time_format.localeKoKr
Korean (South Korea)
# d3_time_format.localeMkMk
Macedonian (Macedonia)
# d3_time_format.localeNlNl
Dutch (Netherlands)
# d3_time_format.localePlPl
Polish (Poland)
# d3_time_format.localePtBr
Portuguese (Brazil)
# d3_time_format.localeRuRu
Russian (Russia)
# d3_time_format.localeSvSe
Swedish (Sweden)
# d3_time_format.localeZhCn
Chinese (China)