1
var date = new Date()

Output: "Wed Nov 28 2012 14:55:24 GMT-0500 (Eastern Standard Time)"

Want to get rid of the UT and time to output:

"Wed Nov 28 2012"
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
1011 1110
  • 743
  • 2
  • 17
  • 43
  • "so don't be mean" - I'm not an arithmetic average, so I'm not a mean... –  Nov 28 '12 at 20:01
  • Also check out [Working with Dates](http://www.elated.com/articles/working-with-dates/) for more ideas. – Andrew Nov 28 '12 at 20:04

2 Answers2

5

You may use toDateString():

new Date().toDateString();  // "Wed Nov 28 2012"
VisioN
  • 143,310
  • 32
  • 282
  • 281
3

Use the toDateString method instead of (implicit) toString:

> new Date().toDateString()
"Wed, 28 Nov 2012"

However, it is implementation-dependent, so if you really need exactly your format you don't get around

var date = new Date();
var day = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][date.getDay()];
var mon = ["Jan", "Feb", "Apr", "Mar", "Mai", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][date.getMonth()];
return day+" "+mon+" "+date.getDate()+" "+date.getFullYear();

Or have a look at one of the many Date libraries and their format methods.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Hm, but what is actually meant by *implementation-dependent*? – VisioN Nov 28 '12 at 20:11
  • 2
    It means that the results can vary between implementations. It means, if you want to write javascript code that works with all browsers, you should not rely on this feature. In opera you get `"Wed, 28 Nov 2012"` and in IE9 you get `"Wed Nov 28 2012"` – Esailija Nov 28 '12 at 20:14
  • 1
    @VisioN: it means that the JavaScript (ECMAScript 262 5th Edition) specification doesn't say *exactly* how the date string must look, just that it [must be "in a convenient, human-readable form"](http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.5.3). – maerics Nov 28 '12 at 20:15
  • @Esailija Alright, I forgot to test Opera. All my browsers output the same, that's why I doubted. However, if the question goes to truncating time and tz only, then simple `toDateString` makes sense. – VisioN Nov 28 '12 at 20:19
  • What makes sense is to use a date library, so that the format can be whatever you want and be changed easily. :P – Esailija Nov 28 '12 at 20:20
  • @Esailija: Just was searching for the link :-) – Bergi Nov 28 '12 at 20:26