fiddling with d3.js date format

by Igor Cuckovic

JavaScript

var format = d3.time.format("%d %b %Y");
//format.parse("2011-01-01"); // returns a Date
var result = format(new Date()); // returns a string



function daysLeftInQuarter(d) {
  d = d || new Date();
  var qEnd = new Date(d);
  qEnd.setMonth(qEnd.getMonth() + 3 - qEnd.getMonth() % 3, 0);
  return Math.floor((qEnd - d) / 8.64e7);
}

//d3.select("body").append("div").html(daysLeftInQuarter());


var today = new Date();
var month = today.getMonth() + 1;
var qtrMonth = (month <= 3) ? 3 : (month <= 6) ? 6 : (month <= 9) ? 9 : 12;
var qtrLastDay = (qtrMonth == 3 || qtrMonth == 12) ? 31 : 30;
var qtrDate = new Date(Date.parse(qtrMonth + "/" + qtrLastDay + "/" + today.getFullYear()));

//get the difference between the 2 dates in milliseconds
var dateDiff = qtrDate.getTime() - today.getTime();

//convert the dateDiff to days and round up to the nearest day (rounding up ensures that the time that has elapsed in the current day is still counted)
dateDiff = Math.ceil(dateDiff / 1000 / 60 / 60 / 24);

// display day left
//document.write(dateDiff + " days until quarter end on " + qtrDate);

d3.select("body").append("div").html(dateDiff);