d3/d3-time-format

Name: d3-time-format

Owner: D3

Description: Parse and format times, inspired by strptime and strftime.

Created: 2015-06-29 21:57:18.0

Updated: 2018-01-11 08:22:32.0

Pushed: 2017-11-21 21:49:20.0

Homepage:

Size: 156

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits
Rob Brackett2015-10-17 03:22:09.02
Kristoffer2017-08-14 12:54:17.01
Mike Bostock2017-11-21 21:48:55.0134
Mehdi El Fadil2016-08-21 10:12:53.01
Yuichi Yazaki2015-09-18 14:22:16.02
Wojtek Kruk2016-02-19 22:40:55.01
Brian Mitchell2017-10-07 19:44:05.07

Other Committers

UserEmailMost Recent Commit# Commits
Filiz Alt?ntürkfiliz.altinturk@nesine.com2017-11-21 10:57:24.01
Víctor Adriánvictor@stormy2016-10-06 03:47:50.01

README

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 formatter from a specifier (a string with the desired format directives, indicated by %); then pass a date to the formatter, which returns a string. For example, to convert the current date to a human-readable string:

formatTime = d3.timeFormat("%B %d, %Y");
atTime(new Date); // "June 30, 2015"

Likewise, to convert a string back to a date, create a parser:

parseTime = d3.timeParse("%B %d, %Y");
eTime("June 30, 2015"); // Tue Jun 30 2015 00:00:00 GMT-0700 (PDT)

You can implement more elaborate conditional time formats, too. For example, here?s a multi-scale time format using time intervals:

formatMillisecond = d3.timeFormat(".%L"),
formatSecond = d3.timeFormat(":%S"),
formatMinute = d3.timeFormat("%I:%M"),
formatHour = d3.timeFormat("%I %p"),
formatDay = d3.timeFormat("%a %d"),
formatWeek = d3.timeFormat("%b %d"),
formatMonth = d3.timeFormat("%B"),
formatYear = d3.timeFormat("%Y");

tion multiFormat(date) {
turn (d3.timeSecond(date) < date ? formatMillisecond
  : d3.timeMinute(date) < date ? formatSecond
  : d3.timeHour(date) < date ? formatMinute
  : d3.timeDay(date) < date ? formatHour
  : d3.timeMonth(date) < date ? (d3.timeWeek(date) < date ? formatDay : formatWeek)
  : d3.timeYear(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. You can also load directly from d3js.org, either as a standalone library or as part of D3 4.0. AMD, CommonJS, and vanilla environments are supported. In vanilla, a d3 global is exported:

ipt src="https://d3js.org/d3-time.v1.min.js"></script>
ipt src="https://d3js.org/d3-time-format.v2.min.js"></script>
ipt>

format = d3.timeFormat("%x");

ript>

Locale files are hosted on unpkg and can be loaded using d3.json. For example, to set Russian as the default locale:

son("https://unpkg.com/d3-time-format@2/locale/ru-RU.json", function(error, locale) {
 (error) throw error;

.timeFormatDefaultLocale(locale);

r format = d3.timeFormat("%c");

nsole.log(format(new Date)); // ???????????,  5 ??????? 2016 ?. 10:31:59

Try d3-time-format in your browser.

API Reference

# d3.timeFormat(specifier) <>

An alias for locale.format on the default locale.

# d3.timeParse(specifier) <>

An alias for locale.parse on the default locale.

# d3.utcFormat(specifier) <>

An alias for locale.utcFormat on the default locale.

# d3.utcParse(specifier) <>

An alias for locale.utcParse on the default locale.

# d3.isoFormat <>

The full ISO 8601 UTC time formatter. Where available, this method will use Date.toISOString to format.

# d3.isoParse <>

The full ISO 8601 UTC time parser. Where available, this method will use 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 parser function:

strictIsoParse = d3.utcParse("%Y-%m-%dT%H:%M:%S.%LZ");

# locale.format(specifier) <>

Returns a new formatter for the given string specifier. The specifier string may contain the following directives:

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. For example, 2015-52 and 2016-00 represent Monday, December 28, 2015, while 2015-53 and 2016-01 represent Monday, January 4, 2016. This differs from the ISO week date specification (%V), which uses a more complicated definition!

For %V, per the strftime man page:

In this system, weeks start on a Monday, and are numbered from 01, for the first week, up to 52 or 53, for the last week. Week 1 is the first week where four or more days fall within the new year (or, synonymously, week 01 is: the first week of the year that contains a Thursday; or, the week that has 4 January in it).

The % sign indicating a directive may be immediately followed by a padding modifier:

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.)

The returned function formats a specified date, returning the corresponding string.

formatMonth = d3.timeFormat("%B"),
formatDay = d3.timeFormat("%A"),
date = new Date(2014, 4, 1); // Thu May 01 2014 00:00:00 GMT-0700 (PDT)

atMonth(date); // "May"
atDay(date); // "Thursday"

# locale.parse(specifier) <>

Returns a new parser for the given string specifier. The specifier string may contain the same directives as locale.format. The %d and %e directives are considered equivalent for parsing.

The returned function parses a 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.

# locale.utcFormat(specifier) <>

Equivalent to locale.format, except all directives are interpreted as Coordinated Universal Time (UTC) rather than local time.

# locale.utcParse(specifier) <>

Equivalent to locale.parse, except all directives are interpreted as Coordinated Universal Time (UTC) rather than local time.

Locales

# d3.timeFormatLocale(definition) <>

Returns a locale object for the specified definition with locale.format, locale.parse, locale.utcFormat, locale.utcParse methods. The definition must include the following properties:

For an example, see Localized Time Axis II.

# d3.timeFormatDefaultLocale(definition) <>

Equivalent to d3.timeFormatLocale, except it also redefines d3.timeFormat, d3.timeParse, d3.utcFormat and d3.utcParse to the new locale?s locale.format, locale.parse, locale.utcFormat and locale.utcParse. If you do not set a default locale, it defaults to U.S. English.

For an example, see Localized Time Axis.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.