tags:

views:

288

answers:

4

how to display a date as 2/25/2007 format in javascript, if i have date object

+3  A: 

This would work:

[date.getMonth() + 1, date.getDay(), date.getFullYear()].join('/')
Reinis I.
date.getMonth() returns from 0-11 so you need to add 1 to it
TheVillageIdiot
You're right. I've always been puzzled by that.
Reinis I.
A: 
(date.getMonth() + 1) + "/" + date.getDay() + "/" + date.getFullYear();
Amarghosh
getYear() - Returns the year, as a two-digit or a three/four-digit number, depending on the browser. Use getFullYear() instead !! (from w3schools.com)
TheVillageIdiot
done. corrected the month too.
Amarghosh
+5  A: 
function formatDate(a) //pass date object
{
  return (a.getMonth() + 1) + "/" + a.getDate() + "/" +  a.getFullYear();
}
TheVillageIdiot
That's d/m/Y, not m/d/Y, though.
Reinis I.
what month is 25 anyway?
TheVillageIdiot
@TheVillageIdiot: I assume it's the US way of doing it, m/d/Y
Svish
US dates style is weird. It does my head in. I stick with YYYY-MM-DD.
TRiG
A: 
function DateToSting(dte){    
    return ( ((dte) && (typeof(dte.format)=="function")) ? dte.format("MM/dd/yyyy") : "Not a Date Object" );
}

ASP.net AJAX has a format method protyped to the Date object:
http://stackoverflow.com/questions/414639/asp-net-ajax-and-datetime-format-on-the-client
also can be used without AJAX.Net:
http://blog.stevenlevithan.com/archives/date-time-format/comment-page-3

Kenneth J
This is overkill to inject the ASP.NET AJAX library into this solution. There are parts built in JavaScript for this.
Chris Love