admin管理员组

文章数量:1328013

I'm creating a simple table using data fetched from SharePoint. everything works perfectly in Google Chrome, but I'm having some issues with Internet Explorer 11. the date is pulled from SharePoint in this format:

2015-03-17T00:00:00

The portion of the code that deals with this is:

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 = "";}

As I mentioned this works perfectly in Chrome and the date display in the MM/DD/YYYY format. But in IE shows like this: "Monday, March 16, 2015 8:00:00 PM". What am I doing wrong here? I could give moment.js a try, but I feel that it is not necessary to add it for just this when it is already partially working. Thanks in advance.

I'm creating a simple table using data fetched from SharePoint. everything works perfectly in Google Chrome, but I'm having some issues with Internet Explorer 11. the date is pulled from SharePoint in this format:

2015-03-17T00:00:00

The portion of the code that deals with this is:

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 = "";}

As I mentioned this works perfectly in Chrome and the date display in the MM/DD/YYYY format. But in IE shows like this: "Monday, March 16, 2015 8:00:00 PM". What am I doing wrong here? I could give moment.js a try, but I feel that it is not necessary to add it for just this when it is already partially working. Thanks in advance.

Share Improve this question asked Jul 21, 2015 at 14:18 cubanGuycubanGuy 1,2965 gold badges28 silver badges51 bronze badges 1
  • You should use moment.js , or maybe trying to change de format in the nav like this: eightforums./attachments/general-support/… – Hackerman Commented Jul 21, 2015 at 14:31
Add a ment  | 

2 Answers 2

Reset to default 6

Internet Explorer handles date objects differently than other browsers. Without going into unnecessary details, why are you not just creating a new Date object of the return value from SharePoint, and then convert it to your desired locale format?

The code below

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);

outputs

Formated date is: ‎3‎/‎17‎/‎2015

in IE11.

Add .replace(/[^ -~]/g,'') toLocaleString for IE.

The code will look like

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);

output

Formated date is: ‎3‎/‎17‎/‎2015

本文标签: javascriptFormatting a date fetched using REST ajax call from SharePointStack Overflow