2

I have a raw date appearing in the format 2011-08-13T10:38:27, I want this to be converted to the mm/dd/yy format using javascript and remove the extra values like time etc.

Is there any way to get the date in this format?

Thanks, Ronan

5 Answers5

2

You can convert the date very simply to a Date object by calling

var date = new Date("2011-08-13T10:38:27");

Then output it using

var dateString = (date.getMonth()+1).toString() + '/' + date.getDate().toString() + '/' +  date.getFullYear().toString();

Please read the previous posts before posting a new question. Format date to MM/dd/yyyy in javascript

Community
  • 1
  • 1
Phil Bozak
  • 2,792
  • 1
  • 19
  • 27
  • Thanks for the solution Man but when I do: var date = new Date(2011-08-13T10:38:27); I get the value of date as Sat Aug 13 2011 16:08:27 GMT+0530 and then when i perform date.tostring("mm/dd/yy"), its values turns out to be an invalid date :( –  Feb 15 '13 at 06:40
  • 2
    +1 on parsing part... JavaScript does not support formatting strings - one need to construct it by hand or use some library for it. – Alexei Levenkov Feb 15 '13 at 06:41
0
var from = new Date(ur_date);</br>
var date =  " DATE('"+(from.getMonth()+1)+"-"+from.getDate()+"-"+from.getFullYear()+"')";</br>

Cheers...

Sundar G
  • 1,069
  • 1
  • 11
  • 29
0
var date = new Date('2011-08-13T10:38:27');
alert((date.getMonth()+1).toString() + '/' + date.getDate().toString() + '/' +  date.getFullYear().toString());
Talha
  • 18,898
  • 8
  • 49
  • 66
0

use substr:

var dateyear="2011-08-13T10:38:27";
var year=dateyear.substr(0,dateyear.indexOf(-)-1);
var month=dateyear.substr(dateyear.indexOf(-)+1,dateyear.indexOf(-)+3)
var date=dateyear.substr(dateyear.indexOf(-)+4,dateyear.indexOf(-).length);

alert(month+"-"+date+"-"+year);
K.P
  • 66
  • 7
-1

Try this function

function formatDate(dateparam) {
    var dateObj = new Date(Date.parse(dateparam));

    var date = dateObj.getDate();
    date = (date.toString().length == 1) ? "0" + date : date;

    var month = dateObj.getMonth();
    month++;
    month = (month.toString().length == 1) ? "0" + month : month;

    var year = dateObj.getFullYear();

    return month + "/" + date + "/" + year;
}

document.write(formatDate("2011-08-13 05:38:27"));
// returns 08/13/2011
icaru12
  • 1,522
  • 16
  • 21