簡體   English   中英

格式化使用來自 SharePoint 的 REST ajax 調用獲取的日期

[英]Formatting a date fetched using REST ajax call from SharePoint

我正在使用從 SharePoint 獲取的數據創建一個簡單的表。 在 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 創建一個返回值的新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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM