Remove time from GMT time format

2019-04-20 22: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.