Get date using javascript in this format [MM/DD/YY]

user1779796 picture user1779796 · Nov 1, 2012 · Viewed 49.9k times · Source


how can I get the date in this format [mm/dd/yy] using javascript. I am struggling to get the 'year' to a 2 digit figure as opposed to the full 4 digits.

Thanks!

Answer

Asad Saeeduddin picture Asad Saeeduddin · Nov 1, 2012
var date = new Date();
var datestring = ("0" + (date.getMonth() + 1).toString()).substr(-2) + "/" + ("0" + date.getDate().toString()).substr(-2)  + "/" + (date.getFullYear().toString()).substr(2);

This guarantees 2 digit dates and months.