1

How can i get the selected date with milliseconds?

I want ISO 8601 UTC format (%Y-%m-%dT%H:%M:%S.%LZ)

let e = d3.event.selection.map(x3.invert);
let s = d3.event.selection || x3.range();
console.log(e[0], e[1]); 
console.log(s[0], s[1]);

// Sun Jul 23 2017 13:32:33 GMT-0300 (-03) Fri Oct 13 2017 08:27:54 GMT-0300 (-03)
// 1037.59375 1040.59375

current format

I'm using the timeParse function to generate the lines and points:

parseDate = d3.timeParse("%Y-%m-%dT%H:%M:%S.%LZ");

From data object:

lines: [[[1467341999153,0,5],5,10,15,20,25,30,35,40,45,50,60,70,...
// The first point of each line is an array: 
// [unix epoch (milliseconds), Y Value, interval (milliseconds) between each point of the line]

The function to format millis to ISOString from data object

let calcDate = function(baseDate, frequencyDate, dateMultiplier) {
     let _ms = baseDate+(frequencyDate*dateMultiplier);
     let _date = new Date(_ms);
     return _date.toISOString()
}


let lineFunction = d3.line()
    .defined(function(d) { return d; })
    .x(function(d,i) {
          if (Array.isArray(d)) {
               _dateMultiplier = 0;
               _frequencyDate = d[2];
               _baseDate = d[0];
               return x(parseDate(calcDate(_baseDate, _frequencyDate, _dateMultiplier)))
          } else {
               _dateMultiplier++;
               return x(parseDate(calcDate(_baseDate, _frequencyDate, _dateMultiplier)))
          };
    })

Thanks!

1 Answers1

1

SOLVED! It's very simple! Just covert using .toISOString()

console.log(e[0].toISOString(), e[1].toISOString());

Thanks

  • toISOString does the conversion based on the client's time zone. I used the moment.js library to convert exactly what I got with the brush. https://momentjs.com/ moment(e[0]).format("Y-M-d HH:mm:ss.SSS zz") moment(e[1]).format("Y-M-d HH:mm:ss.SSS zz") If anyone knows how to do the conversion with pure javascript (i need a human readable date with milliseconds), please show me how. – Marcelo Galvão Jan 23 '18 at 01:25
  • If you have another question, then post another question. However, before you do, make sure it's not going to be quickly closed as a duplicate of [*Where can I find documentation on formatting a date in JavaScript?*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) Generating the required format without a library is straight forward, but a little tedious. Do you really want "Y-M-d HH:mm:ss.SSS zz" (which is something of a mess) or "YYYY-MM-DDTHH:mm:ss.SSSzz" (which is ISO 8601 compliant, based on moment.js formatting tokens)? – RobG Jan 23 '18 at 05:25