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"
2
  • "so don't be mean" - I'm not an arithmetic average, so I'm not a mean... Commented Nov 28, 2012 at 20:01
  • Also check out Working with Dates for more ideas. Commented Nov 28, 2012 at 20:04

2 Answers 2

5

You may use toDateString():

new Date().toDateString();  // "Wed Nov 28 2012"
Sign up to request clarification or add additional context in comments.

Comments

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.

6 Comments

Hm, but what is actually meant by implementation-dependent?
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"
@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".
@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.
What makes sense is to use a date library, so that the format can be whatever you want and be changed easily. :P
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.