Skip to content

d3-time

在可视化时间序列数据、分析时间模式或处理一般时间时,传统时间单位的不规则性很快就会显现出来。例如,在 公历 中,大多数月份有 31 天,但有些月份有 28、29 或 30 天;大多数年份有 365 天,但 闰年 有 366 天;对于 日光保存,大多数日子是 24 小时,但有些日子是 23 或 25 小时。夏令时惯例因国家/地区而异,这进一步增加了复杂性。世界。

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

由于这些时间特性,执行看似简单的任务可能会很困难。例如,如果你想计算两个日期之间相隔的天数,你不能简单地减去并除以 24 小时(86,400,000 毫秒):

¥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):

js
const start = new Date(2015, 02, 01); // 2015-03-01T00:00
const end = new Date(2015, 03, 01); // 2015-04-01T00:00
const days = (end - start) / 864e5; // 30.958333333333332, oops! 🤯

但是,你可以使用 d3.timeDay.count

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

js
d3.timeDay.count(start, end) // 31 😌

dayinterval 是 d3-time 提供的多个 day 类之一。每个间隔都表示一个常规的时间单位 - hoursweeksmonths 等 - 并具有计算边界日期的方法。例如,d3.timeDay 计算相应日期的午夜(通常为当地时间凌晨 12:00)。除了 roundingcounting 之外,间隔还可以用于生成边界日期数组。例如,要计算当前月份的每个星期日:

¥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:

js
const start = d3.timeMonth.floor(new Date(2015, 0, 15)); // 2015-01-01T00:00
const stop = d3.timeMonth.ceil(new Date(2015, 0, 15)); // 2015-02-01T00:00
const weeks = d3.timeWeek.range(start, stop); // [2015-01-04T00:00, 2015-01-11T00:00, 2015-01-18T00:00, 2015-01-25T00:00]

d3-time 模块未实现自己的日历系统;它只是在 ECMAScript 日期 之上实现了一个便捷的日历数学 API。因此,它忽略闰秒,并且只能在当地时区和 协调世界时 (UTC) 下工作。

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

此模块被 D3 的时间刻度和 D3 的时间格式使用,也可以直接用于执行类似 日历布局 的操作。

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

interval(date) {#_interval}

js
d3.utcMonday() // the latest preceding Monday, UTC time

源代码 · 等同于 interval.floor,但如果未指定日期,则默认为当前时间。例如,d3.timeYear(date) 和 d3.timeYear.floor(date) 是等效的。

¥Source · Equivalent to interval.floor, except if date is not specified, it defaults to the current time. For example, d3.timeYear(date) and d3.timeYear.floor(date) are equivalent.

interval.floor(date) {#interval_floor}

js
d3.utcMonday.floor(new Date()) // the latest preceding Monday, UTC time

源代码 · 返回一个表示 date 之后或等于 date 的最早间隔边界日期的新日期。例如,d3.timeDay.floor(date) 通常返回给定日期当地时间凌晨 12:00。

¥Source · 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.

此方法是幂等的:如果指定的日期已下限为当前间隔,则返回具有相同时间的新日期。此外,返回的日期是相关间隔的最小可表达值,例如 interval.floor(interval.floor(date) - 1)

¥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 preceding 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:

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

这比测试时间是否为凌晨 12:00 更可靠,因为在某些时区,由于夏令时,午夜可能不存在。

¥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) {#interval_round}

js
d3.utcMonday.round(new Date()) // the previous or following Monday, whichever is closer

源代码 · 返回一个等于 date 加上 step 间隔的新日期。例如,如果给定日期在中午或之前,d3.timeDay.round(date) 通常返回给定日期当地时间凌晨 12:00;如果给定日期在中午之后,d3.timeDay.round(date) 通常返回第二天凌晨 12:00。

¥Source · 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) {#interval_ceil}

js
d3.utcMonday.ceil(new Date()) // the following Monday

源代码 · 返回一个表示距离 date 最近间隔边界日期的新日期。例如,d3.timeDay.ceil(date) 通常返回给定日期后一天的当地时间凌晨 12:00。

¥Source · 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.

此方法是幂等的:如果指定的日期已上限为当前间隔,则返回具有相同时间的新日期。此外,返回的日期是关联间隔的最大可表达值,因此 interval.ceil(interval.ceil(date) + 1) 返回以下间隔边界日期。

¥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) {#interval_offset}

js
d3.utcDay.offset(new Date(), 1) // the same time tomorrow

源代码 · 返回一个新的循环生成器。如果未指定 step,则默认为 1。如果 step 为负数,则返回的日期将早于指定日期;如果 step 为零,则返回指定日期的副本;如果 step 不是整数,则为 floored。此方法不会将指定日期四舍五入到间隔。例如,如果 date 是今天下午 5:34,那么 d3.timeDay.offset(date, 1) 将返回明天下午 5:34(即使夏令时发生变化!)。

¥Source · 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) {#interval_range}

js
d3.utcDay.range(new Date("2014-01-01"), new Date("2015-01-01")) // every day in 2014

源代码 · 返回一个日期数组,该数组表示起始点(含)之后或等于起始点(含)且终止点(不含)之前的每个间隔边界。如果指定了 step,则将返回每个 step 边界;例如,对于 d3.timeDay 间隔,步长为 2 的数值将每隔一天返回一次。如果 step 不是整数,则为 floored

¥Source · 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.

返回数组中的第一个日期等于或晚于起始值的最早边界;后续日期按步长间隔依次为 offsetfloored。因此,两个重叠范围可以是一致的。例如,以下范围包含奇数天:

¥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:

js
d3.timeDay.range(new Date(2015, 0, 1), new Date(2015, 0, 7), 2) // [2015-01-01T00:00, 2015-01-03T00:00, 2015-01-05T00:00]

包含偶数天:

¥While this contains even days:

js
d3.timeDay.range(new Date(2015, 0, 2), new Date(2015, 0, 8), 2) // [2015-01-02T00:00, 2015-01-04T00:00, 2015-01-06T00:00]

要在指定步长时使范围保持一致,请改用 interval.every

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

为方便起见,interval.range 的别名也以相应间隔的复数形式提供,例如 utcMondays

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

interval.filter(test) {#interval_filter}

源代码 · 返回一个使用指定测试函数过滤该区间子集的新区间。测试函数接收一个日期,当且仅当指定日期被视为间隔的一部分时,该函数才返回 true。例如,要创建一个间隔,返回每月的 1 日、11 日、21 日和 31 日(如果存在):

¥Source · 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:

js
d3.timeDay.filter((d) => (d.getDate() - 1) % 10 === 0)

返回的过滤间隔不支持 interval.count。另请参阅 interval.every

¥The returned filtered interval does not support interval.count. See also interval.every.

interval.every(step) {#interval_every}

js
d3.unixDay.every(3)

源代码 · 返回此间隔的 filtered 视图,代表每个步骤日期。step 的含义取决于 field 函数定义的此区间的父区间。例如,d3.timeMinute.every(15) 返回一个间隔,表示每十五分钟一次,从整点开始::00、:15、:30、:45,等等 请注意,某些时间间隔的日期可能不是均匀分布的;d3.timeDay 的父区间是 d3.timeMonth,因此区间数字会在每个月初重置。如果 step 无效,则返回 null。如果 step 为 1,则返回此间隔。

¥Source · 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.

此方法可与 interval.range 结合使用,以确保两个重叠范围一致。例如,以下范围包含奇数天:

¥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:

js
d3.timeDay.every(2).range(new Date(2015, 0, 1), new Date(2015, 0, 7)) // [2015-01-01T00:00, 2015-01-03T00:00, 2015-01-05T00:00]

就像这个一样:

¥As does this one:

js
d3.timeDay.every(2).range(new Date(2015, 0, 2), new Date(2015, 0, 8)) // [2015-01-03T00:00, 2015-01-05T00:00, 2015-01-07T00:00]

返回的过滤间隔不支持 interval.count。另请参阅 interval.filter

¥The returned filtered interval does not support interval.count. See also interval.filter.

interval.count(start, end) {#interval_count}

源代码 · 返回 start 之后(不含)且 end 之前(含)的区间边界数量。请注意,此行为与 interval.range 略有不同,因为它的目的是返回指定结束日期相对于指定开始日期的从零开始的数字。例如,要计算当前从零开始的日期数字:

¥Source · 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:

js
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:

js
d3.timeSunday.count(d3.timeYear(now), now) // 25

timeInterval(floor, offset, count, field)

js
const utcDay = d3.timeInterval(
  (date) => date.setUTCHours(0, 0, 0, 0), // floor
  (date, step) => date.setUTCDate(date.getUTCDate() + step), // offset
  (start, end) => (end - start) / 864e5, // count
  (date) => date.getUTCDate() - 1 // field
);

源代码 · 根据指定的 floor 和 offset 函数以及可选的 count 函数构造一个新的自定义间隔。

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

floor 函数以单个日期作为参数,并将其向下舍入到最接近的间隔边界。

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

可选的 count 函数接受一个起始日期和一个结束日期,这两个日期已经取整到当前间隔,并返回起始日期(不含)和结束日期(含)之间的边界数。如果未指定计数函数,则返回的间隔不会公开 interval.countinterval.every 方法。注意:由于内部优化,指定的 count 函数不得在其他时间间隔上调用 interval.count。

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

可选的 field 函数接受一个日期,这两个日期已经取整到当前间隔,并返回指定日期的字段值,该值对应于此日期(不含)与最新父边界之间的边界数。例如,对于 d3.timeDay 间隔,这将返回自月初以来的天数。如果未指定字段函数,则默认计算自 1970 年 1 月 1 日 UTC 的 UNIX 纪元以来的区间边界数。field 函数定义了 interval.every 的行为。

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

timeMillisecond

源代码 · 当地时间的毫秒数;最短可用时间单位。

¥Source · Milliseconds in local time; the shortest available time unit.

timeSecond

源代码 · 当地时间的秒数(例如,凌晨 01:23:45.0000);1,000 毫秒。

¥Source · Seconds in local time (e.g., 01:23:45.0000 AM); 1,000 milliseconds.

timeMinute

源代码 · 当地时间的分钟数(例如,凌晨 01:02:00);60 秒。请注意 ECMAScript 忽略闰秒

¥Source · Minutes in local time (e.g., 01:02:00 AM); 60 seconds. Note that ECMAScript ignores leap seconds.

timeHour

源代码 · 当地时间小时数(例如,凌晨 01:00);60 分钟。请注意,将当地时间提前一小时可能会返回相同的小时,或者由于夏令时而跳过一小时。

¥Source · Hours in local time (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.

timeDay

源代码 · 当地时间的天数(例如,2012 年 2 月 7 日凌晨 12:00);通常为 24 小时。由于夏令时,当地时间的天数可能在 23 到 25 小时之间。d3.unixDay 与 d3.utcDay 类似,不同之处在于它计算自 UNIX 纪元(1970 年 1 月 1 日)以来的天数,因此 interval.every 返回均匀间隔的日期,而不是根据日期变化。

¥Source · Days in local time (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.unixDay is like d3.utcDay, except it counts days since the UNIX epoch (January 1, 1970) such that interval.every returns uniformly-spaced dates rather than varying based on day-of-month.

timeWeek

源代码 · d3.timeSunday 的别名;7 天,通常为 168 小时。由于夏令时,当地时间的周数可能在 167 到 169 小时之间。

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

timeSunday

源代码 · 基于当地时间的周日周数(例如,2012 年 2 月 5 日凌晨 12:00)。

¥Source · Sunday-based weeks in local time (e.g., February 5, 2012 at 12:00 AM).

timeMonday

源代码 · 当地时间中基于星期一的星期(例如,2012 年 2 月 6 日凌晨 12:00)。

¥Source · Monday-based weeks in local time (e.g., February 6, 2012 at 12:00 AM).

timeTuesday

源代码 · 以当地时间周二为基准的周数(例如,2012 年 2 月 7 日凌晨 12:00)。

¥Source · Tuesday-based weeks in local time (e.g., February 7, 2012 at 12:00 AM).

timeWednesday

源代码 · 以当地时间周三为基准的周(例如,2012 年 2 月 8 日凌晨 12:00)。

¥Source · Wednesday-based weeks in local time (e.g., February 8, 2012 at 12:00 AM).

timeThursday

源代码 · 以当地时间周四为基准的周数(例如,2012 年 2 月 9 日凌晨 12:00)。

¥Source · Thursday-based weeks in local time (e.g., February 9, 2012 at 12:00 AM).

timeFriday

源代码 · 基于当地时间的星期五的星期(例如,2012 年 2 月 10 日凌晨 12:00)。

¥Source · Friday-based weeks in local time (e.g., February 10, 2012 at 12:00 AM).

timeSaturday

源代码 · 基于当地时间的星期六的星期(例如,2012 年 2 月 11 日凌晨 12:00)。

¥Source · Saturday-based weeks in local time (e.g., February 11, 2012 at 12:00 AM).

timeMonth

源代码 · 当地时间中的月份(例如,2012 年 2 月 1 日凌晨 12:00);范围从 28 到 31 天。

¥Source · Months in local time (e.g., February 1, 2012 at 12:00 AM); ranges from 28 to 31 days.

timeYear

源代码 · 当地时间年份(例如,2012 年 1 月 1 日凌晨 12:00);范围从 365 到 366 天。

¥Source · Years in local time (e.g., January 1, 2012 at 12:00 AM); ranges from 365 to 366 days.

utcMillisecond

源代码 · UTC 时间的毫秒数;最短可用时间单位。

¥Source · Milliseconds in UTC time; the shortest available time unit.

utcSecond

源代码 · UTC 时间的秒数(例如,凌晨 01:23:45.0000);1,000 毫秒。

¥Source · Seconds in UTC time (e.g., 01:23:45.0000 AM); 1,000 milliseconds.

utcMinute

源代码 · UTC 时间的分钟数(例如,凌晨 01:02:00);60 秒。请注意 ECMAScript 忽略闰秒

¥Source · Minutes in UTC time (e.g., 01:02:00 AM); 60 seconds. Note that ECMAScript ignores leap seconds.

utcHour

源代码 · UTC 时间的小时数(例如,凌晨 01:00);60 分钟。

¥Source · Hours in UTC time (e.g., 01:00 AM); 60 minutes.

utcDay

源代码 · UTC 时间的天数(例如,2012 年 2 月 7 日凌晨 12:00);24 小时。

¥Source · Days in UTC time (e.g., February 7, 2012 at 12:00 AM); 24 hours.

utcWeek

源代码 · d3.timeSunday 的别名;7 天 168 小时。

¥Source · Alias for d3.timeSunday; 7 days and 168 hours.

utcSunday

源代码 · 基于 UTC 时间的周日周数(例如,2012 年 2 月 5 日凌晨 12:00)。

¥Source · Sunday-based weeks in UTC time (e.g., February 5, 2012 at 12:00 AM).

utcMonday

源代码 · UTC 时间中基于星期一的星期(例如,2012 年 2 月 6 日凌晨 12:00)。

¥Source · Monday-based weeks in UTC time (e.g., February 6, 2012 at 12:00 AM).

utcTuesday

源代码 · 以 UTC 时间周二为基准的周数(例如,2012 年 2 月 7 日凌晨 12:00)。

¥Source · Tuesday-based weeks in UTC time (e.g., February 7, 2012 at 12:00 AM).

utcWednesday

源代码 · 以 UTC 时间周三为基准的周(例如,2012 年 2 月 8 日凌晨 12:00)。

¥Source · Wednesday-based weeks in UTC time (e.g., February 8, 2012 at 12:00 AM).

utcThursday

源代码 · UTC 时间基于星期四的周(例如,2012 年 2 月 9 日凌晨 12:00)。

¥Source · Thursday-based weeks in UTC time (e.g., February 9, 2012 at 12:00 AM).

utcFriday

源代码 · 基于 UTC 时间的星期五的星期(例如,2012 年 2 月 10 日凌晨 12:00)。

¥Source · Friday-based weeks in UTC time (e.g., February 10, 2012 at 12:00 AM).

utcSaturday

源代码 · 基于 UTC 时间的星期六的星期(例如,2012 年 2 月 11 日凌晨 12:00)。

¥Source · Saturday-based weeks in UTC time (e.g., February 11, 2012 at 12:00 AM).

utcMonth

源代码 · UTC 时间中的月份(例如,2012 年 2 月 1 日凌晨 12:00);范围从 28 到 31 天。

¥Source · Months in UTC time (e.g., February 1, 2012 at 12:00 AM); ranges from 28 to 31 days.

utcYear

源代码 · UTC 时间年份(例如,2012 年 1 月 1 日凌晨 12:00);范围从 365 到 366 天。

¥Source · Years in UTC time (e.g., January 1, 2012 at 12:00 AM); ranges from 365 to 366 days.

unixDay

d3.utcDay 类似,不同之处在于它计算自 UNIX 纪元(1970 年 1 月 1 日)以来的天数,因此 interval.every 返回的是均匀分布的日期,而不是根据月份中的日期变化。

¥Like d3.utcDay, except it counts days since the UNIX epoch (January 1, 1970) such that interval.every returns uniformly-spaced dates rather than varying based on day-of-month.

timeMilliseconds(start, stop, step)

d3.timeMillisecond.range 的别名。

¥Alias for d3.timeMillisecond.range.

timeSeconds(start, stop, step)

d3.timeSecond.range 的别名。

¥Alias for d3.timeSecond.range.

timeMinutes(start, stop, step)

d3.timeMinute.range 的别名。

¥Alias for d3.timeMinute.range.

timeHours(start, stop, step)

d3.timeHour.range 的别名。

¥Alias for d3.timeHour.range.

timeDays(start, stop, step)

d3.timeDay.range 的别名。

¥Alias for d3.timeDay.range.

timeWeeks(start, stop, step)

d3.timeWeek.range 的别名。

¥Alias for d3.timeWeek.range.

timeSundays(start, stop, step)

d3.timeSunday.range 的别名。

¥Alias for d3.timeSunday.range.

timeMondays(start, stop, step)

d3.timeMonday.range 的别名。

¥Alias for d3.timeMonday.range.

timeTuesdays(start, stop, step)

d3.timeTuesday.range 的别名。

¥Alias for d3.timeTuesday.range.

timeWednesdays(start, stop, step)

d3.timeWednesday.range 的别名。

¥Alias for d3.timeWednesday.range.

timeThursdays(start, stop, step)

d3.timeThursday.range 的别名。

¥Alias for d3.timeThursday.range.

timeFridays(start, stop, step)

d3.timeFriday.range 的别名。

¥Alias for d3.timeFriday.range.

timeSaturdays(start, stop, step)

d3.timeSaturday.range 的别名。

¥Alias for d3.timeSaturday.range.

timeMonths(start, stop, step)

d3.timeMonth.range 的别名。

¥Alias for d3.timeMonth.range.

timeYears(start, stop, step)

d3.timeYear.range 的别名。

¥Alias for d3.timeYear.range.

utcMilliseconds(start, stop, step)

d3.utcMillisecond.range 的别名。

¥Alias for d3.utcMillisecond.range.

utcSeconds(start, stop, step)

d3.utcSecond.range 的别名。

¥Alias for d3.utcSecond.range.

utcMinutes(start, stop, step)

d3.utcMinute.range 的别名。

¥Alias for d3.utcMinute.range.

utcHours(start, stop, step)

d3.utcHour.range 的别名。

¥Alias for d3.utcHour.range.

utcDays(start, stop, step)

d3.utcDay.range 的别名。

¥Alias for d3.utcDay.range.

utcWeeks(start, stop, step)

d3.utcWeek.range 的别名。

¥Alias for d3.utcWeek.range.

utcSundays(start, stop, step)

d3.utcSunday.range 的别名。

¥Alias for d3.utcSunday.range.

utcMondays(start, stop, step)

d3.utcMonday.range 的别名。

¥Alias for d3.utcMonday.range.

utcTuesdays(start, stop, step)

d3.utcTuesday.range 的别名。

¥Alias for d3.utcTuesday.range.

utcWednesdays(start, stop, step)

d3.utcWednesday.range 的别名。

¥Alias for d3.utcWednesday.range.

utcThursdays(start, stop, step)

d3.utcThursday.range 的别名。

¥Alias for d3.utcThursday.range.

utcFridays(start, stop, step)

d3.utcFriday.range 的别名。

¥Alias for d3.utcFriday.range.

utcSaturdays(start, stop, step)

d3.utcSaturday.range 的别名。

¥Alias for d3.utcSaturday.range.

utcMonths(start, stop, step)

d3.utcMonth.range 的别名。

¥Alias for d3.utcMonth.range.

utcYears(start, stop, step)

d3.utcYear.range 的别名。

¥Alias for d3.utcYear.range.

timeTicks(start, stop, count)

源代码 · 等同于 d3.utcTicks,但使用当地时间。

¥Source · Equivalent to d3.utcTicks, but in local time.

timeTickInterval(start, stop, count)

源代码 · 返回在相同参数下 d3.timeTicks 将使用的时间间隔。

¥Source · Returns the time interval that would be used by d3.timeTicks given the same arguments.

utcTicks(start, stop, count)

源代码 · 返回一个包含起始点和终止点(含)之间间隔一定时间的大约 count 个日期的数组。如果 stop 在 start 之前,则按时间倒序返回日期;否则按时间顺序返回日期。考虑以下 UTC 时间间隔:

¥Source · Returns an array of approximately count dates at regular intervals between start and stop (inclusive). If stop is before start, dates are returned in reverse chronological order; otherwise dates are returned in chronological order. The following UTC time intervals are considered:

  • 1 秒

    ¥1 second

  • 5 秒

    ¥5 seconds

  • 15 秒

    ¥15 seconds

  • 30 秒

    ¥30 seconds

  • 1 分钟

    ¥1 minute

  • 5 分钟

    ¥5 minutes

  • 15 分钟

    ¥15 minutes

  • 30 分钟

    ¥30 minutes

  • 1 小时

    ¥1 hour

  • 3 小时

    ¥3 hours

  • 6 小时

    ¥6 hours

  • 12 小时

    ¥12 hours

  • 1 天

    ¥1 day

  • 2 天

    ¥2 days

  • 1 周

    ¥1 week

  • 1 个月

    ¥1 month

  • 3 个月

    ¥3 months

  • 1 年

    ¥1 year

遵循 d3.ticks 的规则,毫秒的倍数(对于小范围)和年的倍数(对于大范围)也会被考虑。使用生成最接近 count 的日期数的间隔。例如:

¥Multiples of milliseconds (for small ranges) and years (for large ranges) are also considered, following the rules of d3.ticks. The interval producing the number of dates that is closest to count is used. For example:

js
const start = new Date("1970-03-01");
const stop = new Date("1996-03-19");
const count = 4;
const ticks = d3.utcTicks(start, stop, count); // [1975-01-01, 1980-01-01, 1985-01-01, 1990-01-01, 1995-01-01]

如果 count 是时间间隔,则此函数的行为与 interval.range 类似,不同之处在于 start 和 stop 都包含在内,并且如果 stop 在 start 之前,则它可能会按时间倒序返回日期。

¥If count is a time interval, this function behaves similarly to interval.range except that both start and stop are inclusive and it may return dates in reverse chronological order if stop is before start.

utcTickInterval(start, stop, count)

源代码 · 返回在相同参数下 d3.utcTicks 将使用的时间间隔。如果没有关联的间隔(例如,当 start 或 stop 无效时),则返回 null。

¥Source · Returns the time interval that would be used by d3.utcTicks given the same arguments. If there is no associated interval, such as when start or stop is invalid, returns null.

js
const start = new Date("1970-03-01");
const stop = new Date("1996-03-19");
const count = 4;
const interval = d3.utcTickInterval(start, stop, count); // d3.utcYear.every(5)