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
2 Answers
Reset to default 6Internet 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
版权声明:本文标题:javascript - Formatting a date fetched using REST ajax call from SharePoint - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742244413a2439038.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论