最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Formatting a date fetched using REST ajax call from SharePoint - Stack Overflow

programmeradmin8浏览0评论

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
发布评论

评论列表(0)

  1. 暂无评论