Epoch Time conversion with D3

This seems like a trivial question, but I am getting a strange error:

“Uncaught TypeError: t.slice is not a function”

when trying to pass a unix timestamp to my code.

var myDate = new Date(1075611600000);
  var format = d3.time.format("%Y").parse; //Trying to return the year.
  console.log(format(myDate)); //error noted above
  console.log(myDate); //Sun Feb 01 2004 00:00:00 GMT-0500 (EST)

Anyone see the obvious mistake I am making? Thanks in advance. I have looked at this example and I do not see what I am doing wrong. I am trying to return only the year.

Answer

format function expects a string and you pass in a Date object which does not have slice function.

I believe this is what you want to achieve:

var format = d3.time.format("%Y-%m-%d");
console.log(format(new Date(1075611600000))); // returns a string

[jsfiddle]

https://github.com/mbostock/d3/wiki/Time-Formatting

Leave a Reply

Your email address will not be published. Required fields are marked *