0

I'm trying to convert date formats in d3.js Here's my code, using d3.js (the dates are being accessed on mouseover of points on the map):

d3.selectAll(".events")
      .on("mouseover", function(d) { 
        console.log(d.startDate)
}

Which is giving me output such as:

2019-02-23

I want it to read Saturday February 23 2019. When I try:

d3.selectAll(".events")
      .on("mouseover", function(d) { 
        var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
        console.log(d.startDate.toLocaleDateString("en-US", options)) 
}

I get an error saying:

d.startDate.toLocaleDateString is not a function
at SVGCircleElement

Curious what I'm doing wrong here.

DiamondJoe12
  • 1,879
  • 7
  • 33
  • 81
  • There are thousands of ways to format a date in various ways. There is already a duplicate that contains the most used formats, just take one that is very close and apply some small changes, then you should get what you want. – Jonas Wilms Feb 15 '19 at 18:10
  • Thanks for the help all. toLocaleDateString() is definitely what I want - thank you. I can't quite get it to work in reading d.startDate in my date, to convert it. It is not recognizing .toLocaleDateString() as a function and not sure why. – DiamondJoe12 Feb 15 '19 at 18:37
  • Maybe it is not a date but a string? Then convert it to a date `new Date("2019-02-23").toLocaleString()` – Jonas Wilms Feb 15 '19 at 18:38
  • Jonas, thanks, I think the issue is trying to get new Date to read in d.startDate (i.e. the 'date' property in my data). When I try: console.log(new Date(d.startDate).toLocaleDateString("en-US", options)) , it just outputs today's date when I hover over every point . So it's working, but it's not reading in the actual date associated with the data. Does that make sense? – DiamondJoe12 Feb 15 '19 at 18:44

0 Answers0