Question

Here is a function to convert a unix datetime to datetime but I still have a problem with parsing the date a specific format . The converted unix date is

((Wed May 14 2014 11:15:12 GMT-0800 (Pacific Standard Time))

apparently I have a problem with the d3.time.format("%a %b %d %Y %I:%M:%S %Z").parse;

this is the function

 <!DOCTYPE html>
 <meta charset="utf-8">
  <script src="http://d3js.org/d3.v3.min.js"></script>
  <script>
  var _parse = d3.time.format("%a %b %d %Y %I:%M:%S %Z").parse;
  var maybeDateString = timeConverter("\/Date(1398898800000+0100)\/");

  console.log(maybeDateString); 

  function timeConverter(maybeDateString){
  maybeDateString= maybeDateString.substring(0, 26);
  maybeDateString= Date(maybeDateString*1000)
  maybeDateString= _parse(maybeDateString);
         return maybeDateString;
}
 console.log(_parse)

 </script>

why I can't console.log(_parse)?

Was it helpful?

Solution

You can get the results you want by using d3.time.format.iso as your date formatting function, and d3.time.format.iso.parse as your parsing function.

These use the built in Javascript ISO parsing functions, and so can handle the standard output from Javascript Date functions (including long timezone names).

In contrast, most d3 date parsing functions are very strict -- the input data must match the specified format exactly or else it returns null. And since the d3 format specifiers have no way of indicating long timezone names or arbitrary strings, your date format was never matching the string version of your Dates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top