Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS Date object - return human readable string without time

I'm working with a string of data in this format: "mm-dd-yy". I convert this to a Date object in this way:

var dateData, dateObject, dateReadable, dateSplit, year, month, day;  dateData = "07-21-14"; //For example  dateSplit = dateData.split('-');  month = dateSplit[0] - 1; day = dateSplit[1]; year = 20 + dateSplit[2];  dateObject = new Date(year, month, day);  dateReadable = dateObject.toUTCString(); //Returns Mon, 21 Jul 2014 04:00:00 GMT 

I would like to return the date (Mon, 21 Jul 2014) without the time (04:00:00 GMT). Is there a different method that will do so? Or a way of calling .toUTCString() to return the date without the time?

like image 200
maxhallinan Avatar asked Jul 15 '14 14:07

maxhallinan


1 Answers

I believe you want .toDateString() or .toLocaleDateString()

http://www.w3schools.com/jsref/jsref_todatestring.asp

In fact, you should also look at Date.parse():

var dateData, dateObject, dateReadable;  dateData = "07-21-14"; //For example  dateObject = new Date(Date.parse(dateData));  dateReadable = dateObject.toDateString(); 
like image 65
S McCrohan Avatar answered Sep 20 '22 08:09

S McCrohan