22

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?

Grady D
  • 1,889
  • 6
  • 30
  • 61
  • 1
    `console.log(new Date('Fri, 18 Oct 2013 11:38:23 GMT').toDateString());` – scniro Jan 09 '15 at 21:36
  • When I do this the GMT date is `Mon Jan 12 00:00:00 GMT 2015` and when I plug it into the `toDateString()` function I end up with `Sun Jan 11 2015`, a day behind. – Grady D Jan 09 '15 at 21:41
  • 1
    try fiddling with `toLocaleString(params)` a bit and provide it desired formatting parameters. – Qwerty Jan 09 '15 at 21:55
  • why not using string functions like substring to strip out everything except date? – Tomer Jan 09 '15 at 22:11
  • There are many threads here on SO dealing with this, one such thread is http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript?lq=1 – Xotic750 Jan 09 '15 at 22:26

8 Answers8

34

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

Gio Asencio
  • 577
  • 5
  • 8
  • 6
    If you work with UTC dates, you should use [setUTCHours](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCHours) because setHours _sets the hours for a specified date according to local time._ – wiktor Feb 15 '17 at 08:33
32

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);
Valor_
  • 3,461
  • 9
  • 60
  • 109
Inanda Menezes
  • 1,796
  • 13
  • 17
3

I'm using this workaround :

// d being your current date with wrong times
new Date(d.getFullYear(), d.getMonth(), d.getDate())
Ellone
  • 3,644
  • 12
  • 40
  • 72
2

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')
j3ff
  • 5,719
  • 8
  • 38
  • 51
Mike
  • 67
  • 11
  • Click this link to know how to write a good answer https://stackoverflow.com/help/how-to-answer – DaniyalAhmadSE Apr 16 '20 at 12:25
  • There might be nothing wrong with your code, I don't have much knowledge about `javascript`. I had got your question for review by stack overflow. I had to find out any issue. And I found that your answer only contains the code but does not have any explanation. You should tell the OP(The asker of the question) that "what is causing the problem?", the actual solution, and should explain "how does your solution work?". – DaniyalAhmadSE Apr 17 '20 at 13:36
2

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

Nixon Kosgei
  • 788
  • 8
  • 13
0

Just cut it with substring:

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

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));
Xotic750
  • 22,914
  • 8
  • 57
  • 79
0

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.