Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript reformat date string

Tags:

javascript

I have this date fromat:

Mon Feb 02 2015 05:18:44 GMT+0000 (UTC) 

How can I reformat it to something more friendlier such as 2/2/2015 I am using javascript.

I tried using .format and dateFormat but they both return undefined value.

How can I do this? I don't want to use any regex please.

like image 961
Max Pain Avatar asked Feb 02 '15 06:02

Max Pain


4 Answers

If you want the simplest way just concat all of the separate parts

var output = dt.getMonth( ) + 1 +'/'+ dt.getDate( ) + '/' +dt.getFullYear( );

There are a few libraries that will handle more advanced stuff if you need it, but that is the lightest way I can think of doing what you are asking.

like image 62
konkked Avatar answered Sep 28 '22 23:09

konkked


Use this awesome library. Moment JS

For your case it would be some thing link

var dateString = 'Mon Feb 02 2015 05:18:44 GMT+0000';
var date = new Moment(dateString);

alert(date.format('MM/dd/YYYY'));
like image 20
Sameer Azazi Avatar answered Sep 29 '22 01:09

Sameer Azazi


var d = new Date(); var n = d.toLocaleDateString();

I think this will give you desired output.

like image 36
Kothari Avatar answered Sep 28 '22 23:09

Kothari


You can pass it to Date Object:

var dateString = "Mon Feb 02 2015 05:18:44 GMT+0000 (UTC)";
var date = new Date(dateString);

date.getDate(); // > 2 (the day number)
date.getMonth(); // > 1 (the month number as 0 is January, 11 is December)

You can also find a lib to do the format job or format yourself.

like image 27
omxian Avatar answered Sep 28 '22 23:09

omxian