我有一些查询SharePoint日期列的JavaScript。格式为:DD/MM/YYYY
,结果返回正常,但设备详细信息下的图片显示的日期除外。
我试图使用Moment.js库来格式化它,但我没有运气。 我认为这就是这一行:
var LifeCycleStart = item.DeviceAvailableFrom;
或这一行我需要使用Moment格式:
$('#devicedetails').append($(txtTitle));
以下完整代码:
function getDeviceDetails() {
var txtTitle = "";
var txtOverview = "";
var txtAccessories = "";
var txtDevicetype = "";
var txtTypicalDeviceUsage ="";
var txtKnownSystemIssues ="";
var txtLifeCycles = "";
var tempLCS2 = "";
var query = "http://collaboration-dev.xxx.com/sites/it/SystemInventory/_vti_bin/listdata.svc/Devices?$expand=LifeCycleStatus&$filter=Id eq " + window.DeviceId + "";
var call = $.ajax({
url: query,
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
call.done(function (data,textStatus, jqXHR){
$.each(data.d.results, function(index, item) {
var tempID = item.Id;
var tempTitle = item.Title;
var LifeCycleStart = item.DeviceAvailableFrom;
var LifeCycleStatus = item.LifeCycleStatusValue;
var DeviceOverView = item.Description;
var AccessDetails = item.Accessories;
var DeviceKind = item.Devicetype;
var Usage = item.TypicalUsage;
//var KnownSystem = item.KnownSystemIssues
txtTitle = "<p>" + LifeCycleStart + "</p> <p>" +
LifeCycleStatus + "</p>";
txtOverview = "<p>" + DeviceOverView + "</p>";
txtAccessories = "<p>" + AccessDetails + "</p>";
txtDevicetype = "<p>" + DeviceKind + "</p>";
txtTypicalDeviceUsage = "<p>" + Usage + "</p>";
});
$('#devicedetails').append($(txtTitle));
$('#deviceoverview').append($(txtOverview));
$('#devicekind').append(txtDevicetype);
$('#deviceacc').append(txtAccessories);
$('#deviceuse').append(txtTypicalDeviceUsage);
});
call.fail(function (jqXHR,textStatus,errorThrown){
alert("Error retrieving data: " + jqXHR.responseText);
});
}
答案 0 :(得分:1)
Momentjs可以解析'/Date(1494460800000)/'
之类的字符串,如解析文档的ASP.NET JSON Date部分所述。
在您的情况下,您可以执行以下操作:
var LifeCycleStart = moment(item.DeviceAvailableFrom).format('DD/MM/YYYY');
使用format
将时刻对象转换为字符串。