Skip to content Skip to sidebar Skip to footer

Remove Time From GMT Time Format

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 everythin

Solution 1:

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


Solution 2:

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

Solution 3:

I'm using this workaround :

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

Solution 4:

You could use Moment.js, a library that provides many helper functions to validate, manipulate, display and format dates and times in JavaScript.

Using Moment.js lib:

var dateString = new Date('Mon Jan 12 00:00:00 GMT 2015');
moment(dateString).format('YYYY-MM-DD HH:mm');

Or simplified:

moment('Mon Jan 12 00:00:00 GMT 2015').format('YYYY-MM-DD HH:mm')

Solution 5:

Well,

Here is my Solution

let dateString = 'Mon May 25 01:07:00 GMT 2020';
let dateObj = new Date(dateString);

console.log(dateObj.toDateString());
// outputs Mon May 25 2020

See its documentation on MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString


Post a Comment for "Remove Time From GMT Time Format"