I have a timestamp in the Y-m-d H:i:s format.
var ts = '2022-11-21 07:43:42';
Using d3.tmeParse, I make it usable for d3:
var parseDateTime = d3.timeParse("%Y-%m-%d %H:%M:%S");
var d3ts = parseDateTime(ts);
Are there any possibilities to manipulate the d3ts variable directly? The only thing I found was the offset function, e.g. for adding one day:
d3.timeDay.offset(d3ts,1);
Can I set e.g. hours, minutes and seconds to zero? Or do I have to return to a string representation using a formatter and manipulate the string and then re-parse?
CodePudding user response:
Once parsed that will be a proper JavaScript date, not a "d3.js date" or timestamp. You can use all JavaScript methods on it:
var ts = '2022-11-21 07:43:42';
var parseDateTime = d3.timeParse("%Y-%m-%d %H:%M:%S");
var d3ts = parseDateTime(ts);
//These are JavaScript methods:
console.log(d3ts.getFullYear())
console.log(d3ts.getDay())
console.log(d3ts.toLocaleString())
//For instance, setting the hour to zero:
d3ts.setHours(0)
console.log(d3ts.toLocaleString())
<script src="https://d3js.org/d3.v7.min.js"></script>
The MDN page has a handy list of date methods you can use: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date