简体   繁体   English

D3.js更改时间格式并将数据导出到csv

[英]D3.js change time format and export data into csv

Have an original CSV(cities_9_2.csv) file like this: 拥有原始的CSV(cities_9_2.csv)文件,如下所示:

date
"Wed, 01 Jul 2015 15:16:45 GMT"
"Wed, 01 Jul 2015 15:22:13 GMT"
"Wed, 01 Jul 2015 15:28:52 GMT"
"Wed, 01 Jul 2015 19:56:27 GMT"

Want to change the time format to 5/16/2016 12:00:00AM , have this d3 code below, the export file code is according to https://github.com/agershun/alasql/wiki/How-to-export-JavaScript-array-info-to-csv-on-client-side . 想要将时间格式更改为5/16/2016 12:00:00AM ,在下面有此d3代码,导出文件代码根据https://github.com/agershun/alasql/wiki/How-to-export -将JavaScript数组信息发送到客户端上的csv

But currently the downloaded csv file is empty, does anyone know why? 但是目前下载的csv文件为空,有人知道为什么吗?

var array_date = [];
var parser = d3.time.format("%Y/%m/%d  %I%P");

d3.csv("cities_9_2.csv", function(data){
  data.forEach(function(d) {
    var theDate = parser.parse(d.date);
    array_date.push(theDate);
  })
});


window.exportData = function exportData() {
    alasql("SELECT * INTO CSV('baba.csv') FROM ?",[array_date]);
}

For this date format in the CSV you don't need a parser, simply do the following. 对于CSV中的此日期格式,您不需要解析器,只需执行以下操作即可。

var array_date = [];

d3.csv("my.csv", function(data){
  console.log(data)
  data.forEach(function(d) {
    var theDate = new Date(d.date);//the will get the correct date
    array_date.push(theDate);
  });
  console.log(array_date)
});

working code here 这里的工作代码

If you want to convert it into a dateformat then do : 如果要将其转换为日期格式,请执行以下操作:

var array_date = [];
var parser = d3.time.format("%Y/%m/%d  %I%P");

d3.csv("my.csv", function(data){
  console.log(data)
  data.forEach(function(d) {
    var theDate = parser(new Date(d.date));
    array_date.push(theDate);
  })
  console.log(array_date)
});

working code here 这里的工作代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM