date - D3.js - get number of days for a certain month -


i'm using d3.js (v3) plot time-series graph , i'm trying figure out amount of ticks need given month (ie, days). can make of the documentation, d3.time-month should return 28 31 days, want, reason i'm missing, can't seem number need.

can help?

this i've got far:

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. 

you're misreading documentation on intervals : number of days in intervals used d3.time.month have length between 28 , 31 functions defined as

interval(date)

alias interval.floor(date). [...]

and

interval.floor(date)

rounds down specified date, returning latest time interval before or equal date. [...]

basically, d3.time.month(date) return first day of month @ midnight, not number of days in month.


how number of days then? far can tell, d3 not expose way length of month given date. of course range of days given month , extract length:

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 ) 

or more efficiently use plain js in answer https://stackoverflow.com/a/1185804/1071630 :

var date = new date(2016, 01, 02); // 2016-02-02 console.log(     new date(date.getfullyear(), date.getmonth()+1, 0).getdate() ) 

var date = new date(2016, 01, 02);    console.log(    d3.time.days(d3.time.month(date), d3.time.month.ceil(date)).length  )    console.log(  	new date(date.getfullyear(), date.getmonth()+1, 0).getdate()  )
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>


Comments