Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date D3.js-获取某个月的天数_Date_Datetime_D3.js - Fatal编程技术网

Date D3.js-获取某个月的天数

Date D3.js-获取某个月的天数,date,datetime,d3.js,Date,Datetime,D3.js,我用它来绘制一个时间序列图,并试图计算出给定月份(即,天)所需的滴答数。根据我的理解,d3.time-month应该返回28到31天的数据,这是我想要的,但是由于某些原因,我显然没有找到我需要的数字 有人能帮忙吗 到目前为止,我得到的是: console.log(date); // Fri Jul 01 2016 00:00:00 GMT+0100 (WEST) monthDays = d3.time.month(date); console.log(currentMonth+" has "

我用它来绘制一个时间序列图,并试图计算出给定月份(即,天)所需的滴答数。根据我的理解,
d3.time-month
应该返回28到31天的数据,这是我想要的,但是由于某些原因,我显然没有找到我需要的数字

有人能帮忙吗

到目前为止,我得到的是:

console.log(date); // Fri Jul 01 2016 00:00:00 GMT+0100 (WEST)

monthDays = d3.time.month(date);

console.log(currentMonth+" has "+monthDays+" days."); // July has Fri Jul 01 2016 00:00:00 GMT+0100 (WEST) days.

您误读了以下文档:使用的间隔天数的长度在28到31之间,但函数定义为

间隔(日期)

间隔楼层(日期)的别名。[……]

interval.floor(日期)

向下舍入指定的日期,返回最新的时间间隔 在日期之前或等于日期。[……]

基本上,
d3.time.month(date)
将返回该月的第一天午夜,而不是该月的天数


那么如何计算天数呢?据我所知,D3没有公开一种方法来获取给定日期的月份长度。当然,您可以获取给定月份的天数范围并提取其长度:

var date = new Date(2016, 01, 02); // 2016-02-02
console.log(
    d3.time.days(d3.time.month(date), d3.time.month.ceil(date)).length
)
或者更有效地使用普通JS,如下面的回答:

var日期=新日期(2016年1月2日);
console.log(
d3.时间.天数(d3.时间.月份(日期),d3.时间.月份.ceil(日期)).长度
)
console.log(
新日期(Date.getFullYear(),Date.getMonth()+1,0).getDate()
)

如果您来这里是想快速找到两个d3.Date对象之间的天数,请查看,特别是
interval.count()
方法。
var date = new Date(2016, 01, 02); // 2016-02-02
console.log(
    new Date(date.getFullYear(), date.getMonth()+1, 0).getDate()
)