d3/d3-time

b898f28 Jan 9, 2018
4 contributors

Users who have contributed to this file

330 lines (204 sloc) 25 KB

d3-time

When visualizing time series data, analyzing temporal patterns, or working with time in general, the irregularities of conventional time units quickly become apparent. In the Gregorian calendar, for example, most months have 31 days but some have 28, 29 or 30; most years have 365 days but leap years have 366; and with daylight saving, most days have 24 hours but some have 23 or 25. Adding to complexity, daylight saving conventions vary around the world.

As a result of these temporal peculiarities, it can be difficult to perform seemingly-trivial tasks. For example, if you want to compute the number of days that have passed between two dates, you can’t simply subtract and divide by 24 hours (86,400,000 ms):

```var start = new Date(2015, 02, 01), // Sun Mar 01 2015 00:00:00 GMT-0800 (PST)
end = new Date(2015, 03, 01); // Wed Apr 01 2015 00:00:00 GMT-0700 (PDT)
(end - start) / 864e5; // 30.958333333333332, oops!```

You can, however, use d3.timeDay.count:

`d3.timeDay.count(start, end); // 31`

The day interval is one of several provided by d3-time. Each interval represents a conventional unit of time—hours, weeks, months, etc.—and has methods to calculate boundary dates. For example, d3.timeDay computes midnight (typically 12:00 AM local time) of the corresponding day. In addition to rounding and counting, intervals can also be used to generate arrays of boundary dates. For example, to compute each Sunday in the current month:

```var now = new Date;
d3.timeWeek.range(d3.timeMonth.floor(now), d3.timeMonth.ceil(now));
// [Sun Jun 07 2015 00:00:00 GMT-0700 (PDT),
//  Sun Jun 14 2015 00:00:00 GMT-0700 (PDT),
//  Sun Jun 21 2015 00:00:00 GMT-0700 (PDT),
//  Sun Jun 28 2015 00:00:00 GMT-0700 (PDT)]```

The d3-time module does not implement its own calendaring system; it merely implements a convenient API for calendar math on top of ECMAScript Date. Thus, it ignores leap seconds and can only work with the local time zone and Coordinated Universal Time (UTC).

This module is used by D3’s time scales to generate sensible ticks, by D3’s time format, and can also be used directly to do things like calendar layouts.

Installing

If you use NPM, `npm install d3-time`. 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:

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

var day = d3.timeDay(new Date);

</script>```

API Reference

# interval(date) <>

Alias for interval.floor. For example, d3.timeYear(date) and d3.timeYear.floor(date) are equivalent.

# interval.floor(date) <>

Returns a new date representing the latest interval boundary date before or equal to date. For example, d3.timeDay.floor(date) typically returns 12:00 AM local time on the given date.

This method is idempotent: if the specified date is already floored to the current interval, a new date with an identical time is returned. Furthermore, the returned date is the minimum expressible value of the associated interval, such that interval.floor(interval.floor(date) - 1) returns the preceeding interval boundary date.

Note that the `==` and `===` operators do not compare by value with Date objects, and thus you cannot use them to tell whether the specified date has already been floored. Instead, coerce to a number and then compare:

```// Returns true if the specified date is a day boundary.
function isDay(date) {
return +d3.timeDay.floor(date) === +date;
}```

This is more reliable than testing whether the time is 12:00 AM, as in some time zones midnight may not exist due to daylight saving.

# interval.round(date) <>

Returns a new date representing the closest interval boundary date to date. For example, d3.timeDay.round(date) typically returns 12:00 AM local time on the given date if it is on or before noon, and 12:00 AM of the following day if it is after noon.

This method is idempotent: if the specified date is already rounded to the current interval, a new date with an identical time is returned.

# interval.ceil(date) <>

Returns a new date representing the earliest interval boundary date after or equal to date. For example, d3.timeDay.ceil(date) typically returns 12:00 AM local time on the date following the given date.

This method is idempotent: if the specified date is already ceilinged to the current interval, a new date with an identical time is returned. Furthermore, the returned date is the maximum expressible value of the associated interval, such that interval.ceil(interval.ceil(date) + 1) returns the following interval boundary date.

# interval.offset(date[, step]) <>

Returns a new date equal to date plus step intervals. If step is not specified it defaults to 1. If step is negative, then the returned date will be before the specified date; if step is zero, then a copy of the specified date is returned; if step is not an integer, it is floored. This method does not round the specified date to the interval. For example, if date is today at 5:34 PM, then d3.timeDay.offset(date, 1) returns 5:34 PM tomorrow (even if daylight saving changes!).

# interval.range(start, stop[, step]) <>

Returns an array of dates representing every interval boundary after or equal to start (inclusive) and before stop (exclusive). If step is specified, then every stepth boundary will be returned; for example, for the d3.timeDay interval a step of 2 will return every other day. If step is not an integer, it is floored.

The first date in the returned array is the earliest boundary after or equal to start; subsequent dates are offset by step intervals and floored. Thus, two overlapping ranges may be consistent. For example, this range contains odd days:

```d3.timeDay.range(new Date(2015, 0, 1), new Date(2015, 0, 7), 2);
// [Thu Jan 01 2015 00:00:00 GMT-0800 (PST),
//  Sat Jan 03 2015 00:00:00 GMT-0800 (PST),
//  Mon Jan 05 2015 00:00:00 GMT-0800 (PST)]```

While this contains even days:

```d3.timeDay.range(new Date(2015, 0, 2), new Date(2015, 0, 8), 2);
// [Fri Jan 02 2015 00:00:00 GMT-0800 (PST),
//  Sun Jan 04 2015 00:00:00 GMT-0800 (PST),
//  Tue Jan 06 2015 00:00:00 GMT-0800 (PST)]```

To make ranges consistent when a step is specified, use interval.every instead.

# interval.filter(test) <>

Returns a new interval that is a filtered subset of this interval using the specified test function. The test function is passed a date and should return true if and only if the specified date should be considered part of the interval. For example, to create an interval that returns the 1st, 11th, 21th and 31th (if it exists) of each month:

`var i = d3.timeDay.filter(function(d) { return (d.getDate() - 1) % 10 === 0; });`

# interval.every(step) <>

Returns a filtered view of this interval representing every stepth date. The meaning of step is dependent on this interval’s parent interval as defined by the field function. For example, d3.timeMinute.every(15) returns an interval representing every fifteen minutes, starting on the hour: :00, :15, :30, :45, etc. Note that for some intervals, the resulting dates may not be uniformly-spaced; d3.timeDay’s parent interval is d3.timeMonth, and thus the interval number resets at the start of each month. If step is not valid, returns null. If step is one, returns this interval.

This method can be used in conjunction with interval.range to ensure that two overlapping ranges are consistent. For example, this range contains odd days:

```d3.timeDay.every(2).range(new Date(2015, 0, 1), new Date(2015, 0, 7));
// [Thu Jan 01 2015 00:00:00 GMT-0800 (PST),
//  Sat Jan 03 2015 00:00:00 GMT-0800 (PST),
//  Mon Jan 05 2015 00:00:00 GMT-0800 (PST)]```

As does this one:

```d3.timeDay.every(2).range(new Date(2015, 0, 2), new Date(2015, 0, 8));
// [Sat Jan 03 2015 00:00:00 GMT-0800 (PST),
//  Mon Jan 05 2015 00:00:00 GMT-0800 (PST),
//  Wed Jan 07 2015 00:00:00 GMT-0800 (PST)]```

# interval.count(start, end) <>

Returns the number of interval boundaries after start (exclusive) and before or equal to end (inclusive). Note that this behavior is slightly different than interval.range because its purpose is to return the zero-based number of the specified end date relative to the specified start date. For example, to compute the current zero-based day-of-year number:

```var now = new Date;
d3.timeDay.count(d3.timeYear(now), now); // 177```

Likewise, to compute the current zero-based week-of-year number for weeks that start on Sunday:

`d3.timeSunday.count(d3.timeYear(now), now); // 25`

# d3.timeInterval(floor, offset[, count[, field]]) <>

Constructs a new custom interval given the specified floor and offset functions and an optional count function.

The floor function takes a single date as an argument and rounds it down to the nearest interval boundary.

The offset function takes a date and an integer step as arguments and advances the specified date by the specified number of boundaries; the step may be positive, negative or zero.

The optional count function takes a start date and an end date, already floored to the current interval, and returns the number of boundaries between the start (exclusive) and end (inclusive). If a count function is not specified, the returned interval does not expose interval.count or interval.every methods. Note: due to an internal optimization, the specified count function must not invoke interval.count on other time intervals.

The optional field function takes a date, already floored to the current interval, and returns the field value of the specified date, corresponding to the number of boundaries between this date (exclusive) and the latest previous parent boundary. For example, for the d3.timeDay interval, this returns the number of days since the start of the month. If a field function is not specified, it defaults to counting the number of interval boundaries since the UNIX epoch of January 1, 1970 UTC. The field function defines the behavior of interval.every.

Intervals

The following intervals are provided:

# d3.timeMillisecond <>
# d3.utcMillisecond

Milliseconds; the shortest available time unit.

# d3.timeSecond <>
# d3.utcSecond

Seconds (e.g., 01:23:45.0000 AM); 1,000 milliseconds.

# d3.timeMinute <>
# d3.utcMinute <>

Minutes (e.g., 01:02:00 AM); 60 seconds. Note that ECMAScript ignores leap seconds.

# d3.timeHour <>
# d3.utcHour <>

Hours (e.g., 01:00 AM); 60 minutes. Note that advancing time by one hour in local time can return the same hour or skip an hour due to daylight saving.

# d3.timeDay <>
# d3.utcDay <>

Days (e.g., February 7, 2012 at 12:00 AM); typically 24 hours. Days in local time may range from 23 to 25 hours due to daylight saving.

# d3.timeWeek <>
# d3.utcWeek <>

Alias for d3.timeSunday; 7 days and typically 168 hours. Weeks in local time may range from 167 to 169 hours due on daylight saving.

# d3.timeSunday <>
# d3.utcSunday <>

Sunday-based weeks (e.g., February 5, 2012 at 12:00 AM).

# d3.timeMonday <>
# d3.utcMonday <>

Monday-based weeks (e.g., February 6, 2012 at 12:00 AM).

# d3.timeTuesday <>
# d3.utcTuesday <>

Tuesday-based weeks (e.g., February 7, 2012 at 12:00 AM).

# d3.timeWednesday <>
# d3.utcWednesday <>

Wednesday-based weeks (e.g., February 8, 2012 at 12:00 AM).

# d3.timeThursday <>
# d3.utcThursday <>

Thursday-based weeks (e.g., February 9, 2012 at 12:00 AM).

# d3.timeFriday <>
# d3.utcFriday <>

Friday-based weeks (e.g., February 10, 2012 at 12:00 AM).

# d3.timeSaturday <>
# d3.utcSaturday <>

Saturday-based weeks (e.g., February 11, 2012 at 12:00 AM).

# d3.timeMonth <>
# d3.utcMonth <>

Months (e.g., February 1, 2012 at 12:00 AM); ranges from 28 to 31 days.

# d3.timeYear <>
# d3.utcYear <>

Years (e.g., January 1, 2012 at 12:00 AM); ranges from 365 to 366 days.

Ranges

For convenience, aliases for interval.range are also provided as plural forms of the corresponding interval.

# d3.timeMilliseconds(start, stop[, step]) <>
# d3.utcMilliseconds(start, stop[, step])

Aliases for d3.timeMillisecond.range and d3.utcMillisecond.range.

# d3.timeSeconds(start, stop[, step]) <>
# d3.utcSeconds(start, stop[, step])

Aliases for d3.timeSecond.range and d3.utcSecond.range.

# d3.timeMinutes(start, stop[, step]) <>
# d3.utcMinutes(start, stop[, step]) <>

Aliases for d3.timeMinute.range and d3.utcMinute.range.

# d3.timeHours(start, stop[, step]) <>
# d3.utcHours(start, stop[, step]) <>

Aliases for d3.timeHour.range and d3.utcHour.range.

# d3.timeDays(start, stop[, step]) <>
# d3.utcDays(start, stop[, step]) <>

Aliases for d3.timeDay.range and d3.utcDay.range.

# d3.timeWeeks(start, stop[, step])
# d3.utcWeeks(start, stop[, step])

Aliases for d3.timeWeek.range and d3.utcWeek.range.

# d3.timeSundays(start, stop[, step]) <>
# d3.utcSundays(start, stop[, step]) <>

Aliases for d3.timeSunday.range and d3.utcSunday.range.

# d3.timeMondays(start, stop[, step]) <>
# d3.utcMondays(start, stop[, step]) <>

Aliases for d3.timeMonday.range and d3.utcMonday.range.

# d3.timeTuesdays(start, stop[, step]) <>
# d3.utcTuesdays(start, stop[, step]) <>

Aliases for d3.timeTuesday.range and d3.utcTuesday.range.

# d3.timeWednesdays(start, stop[, step]) <>
# d3.utcWednesdays(start, stop[, step]) <>

Aliases for d3.timeWednesday.range and d3.utcWednesday.range.

# d3.timeThursdays(start, stop[, step]) <>
# d3.utcThursdays(start, stop[, step]) <>

Aliases for d3.timeThursday.range and d3.utcThursday.range.

# d3.timeFridays(start, stop[, step]) <>
# d3.utcFridays(start, stop[, step]) <>

Aliases for d3.timeFriday.range and d3.utcFriday.range.

# d3.timeSaturdays(start, stop[, step]) <>
# d3.utcSaturdays(start, stop[, step]) <>

Aliases for d3.timeSaturday.range and d3.utcSaturday.range.

# d3.timeMonths(start, stop[, step]) <>
# d3.utcMonths(start, stop[, step]) <>

Aliases for d3.timeMonth.range and d3.utcMonth.range.

# d3.timeYears(start, stop[, step]) <>
# d3.utcYears(start, stop[, step]) <>

Aliases for d3.timeYear.range and d3.utcYear.range.

You can’t perform that action at this time.