How can I turn this Fri Feb 21 2014 00:00:00 GMT-0800 (Pacific Standard Time)
into just this 2014-02-21
using javascript ?

- 25
- 7
-
Break it into bits (split might help) then format them into what you want. – RobG Feb 14 '14 at 23:23
-
1http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript - dupe? – Jake Johnson Feb 14 '14 at 23:23
4 Answers
You can break the string into its parts, then format the bits into what you need:
// Reformat string like: Fri Feb 21 2014 00:00:00 GMT-0800 (Pacific Standard Time)
// do yyyy-mm-dd
function reformatDateString(s) {
function z(n){return (n<10?'0':'') + n;}
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'};
s = s.split(/[ :]/g);
return s[3] + '-' + months[s[1].toLowerCase()] + '-' + z(s[2]);
}
You can use the Date constructor, but it's not necessary here. Using the constructor to parse strings is problematic since the string in the OP doesn't fit the format specified in ES5 (which is not supported by all browsers in use) and parsing is otherwise implementation dependent.
So to use Date you need to parse the parts anyway, resulting in many extra function calls.

- 142,382
- 31
- 172
- 209
Using standard string/array manipulation
var timeStamp = 'Fri Feb 21 2014 00:00:00 GMT-0800 (Pacific Standard Time)',
months = {
Jan: 1,
Feb: 2,
Mar: 3,
Apr: 4,
May: 5,
Jun: 6,
Jul: 7,
Aug: 8,
Sep: 9,
Oct: 10,
Nov: 11,
Dec: 12
},
parts = timeStamp.split(' ', 4).slice(1),
myStamp;
function pad(val) {
if (val < 10) {
val = '0' + val;
}
return val;
}
parts[0] = months[parts[0]];
parts.unshift(parts.pop());
parts[1] = pad(parts[1]);
parts[2] = pad(parts[2]);
mystamp = parts.join('-');
console.log(mystamp);
Output
2014-02-21
On jsFiddle

- 22,914
- 8
- 57
- 79
The date Fri Feb 21 2014 00:00:00 GMT-0800
is in standard RFC 2822 format so you can create a new date using new Date()
passing it as a parameter. This will convert it into UTC (milliseconds since 1/1/1970) which you can manipulate.
You can then convert UTC into ISO 8601 extended format (2014-02-21T00:00:00.000Z
) with the toISOString()
method and get the text before the T
:
var utcDate = new Date('Fri Feb 21 2014 00:00:00 GMT-0800');
var isoExtendedDate = utcDate.toISOString();
var isoSimpleDate = isoExtendedDate.split("T")[0];

- 1
- 1

- 23,209
- 4
- 50
- 65
-
1ES5 does not require implementations to support any format other than a version of ISO 8601. Parsing of other formats is implementation dependent – RobG Feb 14 '14 at 23:42
I'm unable to delete this since the OP chose it as the answer (OP please choose Xotic750's) answer.

- 1
- 1

- 60,826
- 17
- 123
- 123
-
-
-
And if you are lucky enough to have an environment that parses that string correctly. – Xotic750 Feb 14 '14 at 23:25
-
3Parsing non–standard date strings is entirely implementation dependent, far better to manually parse the string. There is a *getFullYear* method that returns the full year. – RobG Feb 14 '14 at 23:25
-
-
Why `year = d.getYear() + 1900` instead of `year = d.getFullYear()`? http://stackoverflow.com/questions/98124/why-does-javascript-getyear-return-108 – Xotic750 Feb 15 '14 at 00:07
-
Does anyone still use browsers today that don't support `getFullYear()`? – helderdarocha Feb 15 '14 at 00:20
-
And if you are, then do you really expect them to successfully parse the string? :) – Xotic750 Feb 15 '14 at 01:05
-
2-1. The poster has had plenty of time to update the post, but it gets a down vote because of dependence on non–standard parsing, may cause a change of date due to time zone differences, use of *getYear* and does not correctly format 2 digit values. – RobG Feb 15 '14 at 09:26