cub*_*Guy 2 javascript ajax sharepoint jquery date
我正在使用从 SharePoint 获取的数据创建一个简单的表。在 Google Chrome 中一切正常,但我在使用 Internet Explorer 11 时遇到了一些问题。日期是以这种格式从 SharePoint 中提取的:
2015-03-17T00:00:00
Run Code Online (Sandbox Code Playgroud)
处理此问题的代码部分是:
var dateReceived = data.d.results[i].DateReceived;
if (dateReceived !== null){dateReceived = new Date(parseInt(dateReceived.replace("/Date(", "").replace(")/", ""), 10)).toLocaleString('en-US', {
year: 'numeric',
month: 'numeric',
day: '2-digit'
});}
else {dateReceived = "";}
Run Code Online (Sandbox Code Playgroud)
正如我提到的,这在 Chrome 中完美运行,日期显示为 MM/DD/YYYY 格式。但在 IE 中显示如下:“Monday, March 16, 2015 8:00:00 PM”。我在这里做错了什么?我可以尝试一下 moment.js,但我觉得当它已经部分工作时,没有必要为此添加它。提前致谢。
Internet Explorer 处理日期对象的方式与其他浏览器不同。在不涉及不必要的细节的情况下,为什么不直接Date从 SharePoint创建一个返回值的新对象,然后将其转换为所需的区域设置格式?
下面的代码
var date = new Date("2015-03-17T00:00:00");
var formatDate = date.toLocaleString('en-US', {year: "numeric", month: "numeric", day: "numeric" });
console.log("Formated date is: " + formatDate);
Run Code Online (Sandbox Code Playgroud)
产出
Formated date is: ?3?/?17?/?2015
在 IE11 中。