javascript .substr中断警报方法

时间:2018-07-19 19:29:19

标签: javascript json

我正在尝试从Microsoft JSON字符串中提取小时和分钟。我浏览了几篇文章,最近一篇是:How do I format a Microsoft JSON date?无济于事。当我尝试遵循提供的示例代码时。

警报不会触发。不知道我在做什么错或丢失。相关代码在这里:

$.ajax({
  type: "GET",
  url: "/TrainActivity/GetDelayDataForEditing/" + "?delayId=" + delId,
  dataType: 'json',
  //data: delId,
  success: function(data) {
    //data = JSON.stringify(data);
    //$("#myDivID").text(JSON.stringify(data));
    //var delayId = delId;

    modal.find('');
    //This is my code based off stack overflow article, and it breaks the alert
    var date = new Date(parseInt(data.beginDelayDateTime.substr(6)));

    //var date = data.beginDelayDateTime.substr(6);
    //var date = new Date(parseInt(jsonDate.substr(6)));

    //this date is okay
    //var date = new Date();
    var unparsedDate = moment(data.beginDelayDateTime);
    var parsedDate = new Date(unparsedDate);
    var hours = parsedDate.getHours;
    var minutes = parsedDate.getMinutes;
    var timeToDisplay = hours + ":" + minutes;
    //var timeToDisplay = JSON.stringify(hours) + ":" + JSON.stringify(minutes);

    alert("Success " +
      //"\ntest date: " + date +
      "\nunparsed date: " + unparsedDate +
      "\nParsed date: " + parsedDate +
      "\nDisplay time: " + timeToDisplay +
      "\nbegin Delay time: " + data.BeginDelayDateTime
    );

    //$('#delays-grid').data('kendoGrid').dataSource.read();
    //$("#delayAddModal").modal("hide");

  },
  error: function() {
    alert("error in Delay Edit");
  }
});

//modal.find(".modal-body").text("Edit the Delay at " + name + " with id " + delId);
modal.find(".modal-footer #delayEditButton").data("guid", delId);
});

data.beginDelayDateTime包含/ Date(1531958520000)/,并且该值(“ / Date(1531958520000)/”)毫无问题地显示在警报中。

1 个答案:

答案 0 :(得分:1)

该问题已通过纠正印刷错误解决。正如epascarello指出BeginDelayDateTime!= beginDelayDateTime。通过更正拼写错误,此问题已得到解决,并且可以正确解析Microsoft JSON日期字符串。非常感谢所有提供帮助的人。