How can I convert this format "Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)"
to just 2014-01-31
in Javascript ?? I know it should be simple but I didnt get it from google

- 123
- 1
- 2
- 7
-
Is this a `Date` object? Take a look at the available methods: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – Guilherme Sehn Dec 13 '13 at 00:04
8 Answers
var d = new Date("Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)");
var str = $.datepicker.formatDate('yy-mm-dd', d);
alert(str);
This requires jQuery UI.

- 20,108
- 1
- 57
- 70
-
-
@user3097566 That's what `d` is in the above code. Replace that with your `Date` object. – Matt Dec 13 '13 at 00:25
-
@Matt—you are relying on non–standard behaviour (parsing a non–standard date string), your code will fail in some browsers. – RobG Dec 13 '13 at 00:47
-
@RobG I don't doubt that it will fail in some browsers, (IE6?) but I tried this in Chrome/FF/IE and it works. Also I spent some time looking at RFC 2822 to see how it is "non-standard" and didn't find anything. Any specifics, if you have the time, to point out how this violates the standard would be appreciated. – Matt Dec 13 '13 at 01:08
-
@Matt—it is "non–standard" in that there is nothing in ECMA-262 or W3C DOM standards or the various HTML/HTML5 specifications that state that browsers must correctly parse that format. – RobG Dec 16 '13 at 02:51
-
All that over head just for formatting a timestamp. You could to that with the native `Date` object. – Ryan Aug 08 '14 at 18:48
Split the string based on the blank spaces. Take the parts and reconstruct it.
function convertDate(d){
var parts = d.split(" ");
var months = {Jan: "01",Feb: "02",Mar: "03",Apr: "04",May: "05",Jun: "06",Jul: "07",Aug: "08",Sep: "09",Oct: "10",Nov: "11",Dec: "12"};
return parts[3]+"-"+months[parts[1]]+"-"+parts[2];
}
var d = "Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)";
alert(convertDate(d));

- 81,153
- 41
- 202
- 273
-
1This is the only answer that comes close. Date strings must be manually parsed, particularly where the provided string is not consistent with ECMA-262 ed 5. It might be better to have the months as lower case and do `toLowerCase()` on the month part of the string (or all upper case and use `toUpperCase()`). – RobG Dec 13 '13 at 00:48
You can do it like this
var date = new Date("Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)");
var year=date.getFullYear();
var month=date.getMonth()+1 //getMonth is zero based;
var day=date.getDate();
var formatted=year+"-"+month+"-"+day;
I see you're trying to format a date. You should totally drop that and use jQuery UI
You can format it like this then
var str = $.datepicker.formatDate('yy-mm-dd', new Date("Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)");
I found Web Developer's Notes helpful in formatting dates

- 7,228
- 9
- 33
- 64
-
-
but where do I give it the value Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time) to convert ? – user3097566 Dec 13 '13 at 00:07
-
In Chrome this results in `"Thu Dec 12 2013 09:06:49 GMT-0500 (Eastern Standard Time)"`, not the string intended. @IlyaBursov: no, this is a matter of string manipulation. – Qantas 94 Heavy Dec 13 '13 at 00:08
-
Do not depend on the Date constructor to correctly parse date strings, it is very problematic. – RobG Dec 13 '13 at 00:45
For things like this it's often good to do a little testing in the browser console.
var date = new Date("Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)");
console.log(date.getFullYear() + '-' + date.getMonth()+1 + '-' + date.getDate())
Ensure you add + 1 to the result of getMonth() because it is zero based.
A similar question was asked here:
Where can I find documentation on formatting a date in JavaScript?
-
Even if the date string is successfully parsed (it won't be in many browsers), the above will not pad single digit day or month numbers which is probably what the OP wants (though not stated). – RobG Dec 13 '13 at 01:09
-
@RobG It appears to work in quite a lot of browsers. Then again, the OP can make the judgement based on the context of their question. Of course if you want to be absolutely safe, manual string manipulation will no doubt be most reliable. – dr4g0nus Dec 13 '13 at 03:36
start_date="14 Feb 2020";
var new_startDate= new Date(start_date);
var date= moment(new_startDate).format('YYYY-MM-DD');
Answer: 2020-02-14
In here you have to use moment.js

- 687
- 4
- 14
- 27

- 11
- 1
The easiest way to convert is
new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'long',
day: '2-digit'
}).format(new Date('Your Date'))
Just Replace 'Your Date' with your complicated date format :)

- 3,925
- 5
- 31
- 54

- 493
- 6
- 8
You can also use the Moment.js library, make sure to give it a search.
Few examples: moment().format('MMMM Do YYYY, h:mm:ss a');
moment().format('dddd');

- 15,750
- 31
- 68
- 83

- 11
- 1
-
You're welcome. Only thing is, this doesn't provide the answer the questioner was looking for -- I think he's looking for the format `2014-01-31`. – Qantas 94 Heavy Dec 13 '13 at 00:22
A trifling refinement:
var date = new Date(value);
var year = date.getFullYear();
var rawMonth = parseInt(date.getMonth()) + 1;
var month = rawMonth < 10 ? '0' + rawMonth : rawmonth;
var rawDay = parseInt(date.getDate());
var day = rawDay < 10 ? '0' + rawDay : rawDay;
console.log(year + '-' + month + '-' + day);

- 41
- 7