Remove time from GMT time format

泪湿孤枕 提交于 2019-12-05 11:13:15

问题


I am getting a date that comes in GMT format, Fri, 18 Oct 2013 11:38:23 GMT. The problem is that the time is messing up the timeline that I am using.

How can I strip out everything except for the actual date?


回答1:


Like this:

var dateString = 'Mon Jan 12 00:00:00 GMT 2015';
dateString = new Date(dateString).toUTCString();
dateString = dateString.split(' ').slice(0, 4).join(' ');
console.log(dateString);



回答2:


If you want to keep using Date and not String you could do this:

var d=new Date(); //your date object
console.log(new Date(d.setHours(0,0,0,0)));

-PS, you don't need a new Date object, it's just an example in case you want to log it to the console.

http://www.w3schools.com/jsref/jsref_sethours.asp




回答3:


I'm using this workaround :

// d being your current date with wrong times
new Date(d.getFullYear(), d.getMonth(), d.getDate())



回答4:


Just cut it with substring:

 var str = 'Fri, 18 Oct 2013 11:38:23 GMT';
 str = str.substring(0,tomorrow.toLocaleString().indexOf(':')-3);



回答5:


In this case you can just manipulate your string without the use of a Date object.

var dateTime = 'Fri, 18 Oct 2013 11:38:23 GMT',
    date = dateTime.split(' ', 4).join(' ');
    
document.body.appendChild(document.createTextNode(date));



回答6:


You can first convert the date to String:

String dateString = String.valueOf(date);

Then apply substring to the String:

dateString.substring(4, 11) + dateString.substring(30);

You need to take care as converting date to String will actually change the date format as well.



来源:https://stackoverflow.com/questions/27869606/remove-time-from-gmt-time-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!