How do I format this date so that the alert displays the date in MM/dd/yyyy format?
Just another option:
DP_DateExtensions Library
Not saying it's any better than the other options, but I like it (of course I'm not completely unbiased).
A simple format would be:
var d = new Date() // Thu Jun 30 2016 12:50:43 GMT-0500 (Central Daylight Time (Mexico))
d.toJSON(); // "2016-06-30T17:50:43.084Z"
You Could try:
date = new Date().toLocaleDateString().split("/")
date[0].length == 1 ? "0" + date[0] : date[0]
date[1].length == 1 ? "0" + date[1] : date[1]
date = date[0] + "/" + date[1] + "/" + date[2]
You have to get old school on it:
Date.prototype.toMMddyyyy = function() {
var padNumber = function(number) {
number = number.toString();
if (number.length === 1) {
return "0" + number;
}
return number;
};
return padNumber(date.getMonth() + 1) + "/"
+ padNumber(date.getDate()) + "/" + date.getFullYear();
};
With a proper library you could internationalize your app for the whole world with just a few lines of code. By default it automatically localizes the date for the browser locale, but you can also define your own patterns:
dojo.date.locale.format(
new Date(2007,2,23,6,6,6),
{datePattern: "yyyy-MM-dd", selector: "date"}
);
// yields: "2007-03-23"
From: Formatting dates and times using custom patterns
add Jquery Ui plugin to your page
alert($.datepicker.formatDate('dd M yy', new Date()));