尝试将Json时间字符串转换为语言环境时间时,返回无效日期

时间:2018-10-15 09:10:53

标签: javascript angularjs datetime

我正在尝试将JSON时间字符串转换为本地时间,如下所示

angular.forEach($scope.calendarData, function(item) {
  item.scheduleArray.startTime = new Date(item.scheduleArray.startTime);
  item.scheduleArray.endTime = new Date(item.scheduleArray.endTime);
});

例如

startTime = "1970-01-01T08:52:00.000Z"

但是当我执行代码时,它会显示无效的日期。我该如何解决这个问题

1 个答案:

答案 0 :(得分:0)

let scheduleArray = {alert: "emailNotification", endTime: "1970-01-01T08:52:00.000Z", expanded: true, startTime: "1970-01-01T11:12:00.000Z", title: "New Schedule"};

let calendarData = [];
calendarData.push({'scheduleArray':scheduleArray});

angular.forEach(calendarData, function(item) {
  item.scheduleArray.startTime = new Date(item.scheduleArray.startTime);
  item.scheduleArray.endTime = new Date(item.scheduleArray.endTime);
  console.log(item.scheduleArray.startTime);
});

console.log(calendarData);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>