Jean Dupont
Jean Dupont

Reputation: 177

Can I Parse time in d3 in this format "%H%M"?

Hiii,

I have a time with this format hhmm, like this : 2010

I have tried this :

var timeFormat = d3.time.format("%H%M");
d.Dep_Time1= timeFormat.parse(d.Dep_Time1);

But it does not work ... :s

Please how can I achieve the above ?

Upvotes: 1

Views: 515

Answers (1)

Gerardo Furtado
Gerardo Furtado

Reputation: 102208

A solution is converting your 4-digit number to a string, in such a way that D3 knows how to parse it:

var times = [2210, 2315, 0817, 1456];
var timeFormat = d3.time.format("%H%M");

times.forEach(function(time){
    time = timeFormat.parse(time.toString());
    console.log(time);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

PS: edit based on @altocumulus comment (see below).

Upvotes: 2

Related Questions