使用JavaScript将日期格式从yyyy-MM-dd'T'HH:mm:ss.SSS'Z'更改为dd-mmm-yyyy

时间:2017-07-13 10:57:59

标签: javascript angularjs date

我想将日期格式从yyyy-MM-dd'T'HH:mm:ss.SSS'Z'更改为dd-mmm-yyyy 当日期来自JSON对象时。我正在使用ng-csv插件下载此JSON。下载功能正常,但我需要使用JavaScript转换日期格式。我有像这样的JSON结构

[{
    "Dates": "2016-09-27T18:30:00.000Z",
    "ABC": 40,
    "PQR": 1,
    "XYZ": 18
}, {
    "Dates": "2016-10-02T18:30:00.000Z",
    "ABC": 43,
    "PQR": 11,
    "XYZ": 8
}, {
    "Dates": "2016-10-03T18:30:00.000Z",
    "ABC": 6,
    "PQR": 76,
    "XYZ": 34
}]

有什么建议吗?在此先感谢。

1 个答案:

答案 0 :(得分:-2)

试试这个

function getDate(dateVal) {

var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];

  var date1 = dateVal.split('T')[0];
  var date = new Date(date1);
  var getDay = date.getDate();
  var getMonth = date.getMonth() + 1;
  var getYear = date.getFullYear();
  alert(getDay +' '+ monthNames[getMonth]  + ' ' +getYear);
  return  getDay +'-'+ monthNames[getMonth] + '-' +getYear
}

getDate('2016-09-27T18:30:00.000Z');

var data = [
 {
"Dates": "2016-09-27T18:30:00.000Z",
"ABC": 40,
"PQR": 1,
"XYZ": 18
},{
"Dates": "2016-10-02T18:30:00.000Z",
"ABC": 43,
"PQR": 11,
"XYZ": 8
},{
"Dates": "2016-10-03T18:30:00.000Z",
"ABC": 6,
"PQR": 76,
"XYZ": 34
}
];
var temp = [];
for(var i=0; i<data.length;i++){

  temp[i] = getDate(data[i].Dates);
}

console.log(temp);

Working Fiddle