d3/d3-time-format

numeric month without leading 0

peterdd opened this issue · 1 comments

The %m format character always adds a leading zero for month <10

PHP has a %n format character for that for example, see https://www.php.net/manual/en/datetime.format.php

In my case I use it for a simple LineChart with 2 xAxis

 const xAxis  = d3.axisBottom(xScale).ticks(width / 40).tickSizeOuter(0).tickFormat(d3.timeFormat('%m'));
 const xAxis2 = d3.axisBottom(xScale).ticks(width / 160).tickSizeOuter(0).tickFormat(d3.timeFormat('%Y'));

linechart-doublexaxis

ugly workaround - changing month numbers after chart is added:

  document.getElementById('chart').append(chart);

  // postprocessing month names on first xAxis
  n=document.getElementById('chart').firstChild.firstChild.childNodes;
  nl=n.length;

  // first is a path-tag
  for(i=1; i<nl; i++){
    if(n[i].lastChild.innerHTML.substring(0,1)=='0'){
      n[i].lastChild.innerHTML=n[i].lastChild.innerHTML.substring(1);
    }
  }

It would by nice if there is a simpler way to set numeric months without leading zeros into axis ticks.

%-m