格式化使用来自 SharePoint 的 REST ajax 调用获取的日期
Formatting a date fetched using REST ajax call from SharePoint
我正在使用从 SharePoint 获取的数据创建一个简单的 table。在 Google Chrome 中一切正常,但我在使用 Internet Explorer 11 时遇到了一些问题。日期以这种格式从 SharePoint 中提取:
2015-03-17T00:00:00
处理此问题的代码部分是:
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 = "";}
正如我所提到的,这在 Chrome 中非常有效,日期显示在 MM/DD/YYYY 格式中。但是在IE中是这样显示的:"Monday, March 16, 2015 8:00:00 PM"。我在这里做错了什么?我可以尝试 moment.js,但我觉得当它已经部分工作时,没有必要为此添加它。提前致谢。
Internet Explorer 处理日期对象的方式与其他浏览器不同。无需深入了解不必要的细节,您为什么不直接从 SharePoint 创建 return 值的新 Date
对象,然后将其转换为所需的区域设置格式?
下面的代码
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);
产出
Formated date is: 3/17/2015
在 IE11 中。
为 IE 添加 .replace(/[^ -~]/g,'')
toLocaleString。
代码看起来像
var date = new Date("2015-03-17T00:00:00");
var formatDate = date.toLocaleString('en-US', {year: "numeric", month: "numeric", day: "numeric" }).replace(/[^ -~]/g,'');
console.log("Formated date is: " + formatDate);
输出
Formated date is: 3/17/2015
我正在使用从 SharePoint 获取的数据创建一个简单的 table。在 Google Chrome 中一切正常,但我在使用 Internet Explorer 11 时遇到了一些问题。日期以这种格式从 SharePoint 中提取:
2015-03-17T00:00:00
处理此问题的代码部分是:
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 = "";}
正如我所提到的,这在 Chrome 中非常有效,日期显示在 MM/DD/YYYY 格式中。但是在IE中是这样显示的:"Monday, March 16, 2015 8:00:00 PM"。我在这里做错了什么?我可以尝试 moment.js,但我觉得当它已经部分工作时,没有必要为此添加它。提前致谢。
Internet Explorer 处理日期对象的方式与其他浏览器不同。无需深入了解不必要的细节,您为什么不直接从 SharePoint 创建 return 值的新 Date
对象,然后将其转换为所需的区域设置格式?
下面的代码
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);
产出
Formated date is: 3/17/2015
在 IE11 中。
为 IE 添加 .replace(/[^ -~]/g,'')
toLocaleString。
代码看起来像
var date = new Date("2015-03-17T00:00:00");
var formatDate = date.toLocaleString('en-US', {year: "numeric", month: "numeric", day: "numeric" }).replace(/[^ -~]/g,'');
console.log("Formated date is: " + formatDate);
输出
Formated date is: 3/17/2015