How can I get the date in this format
1/11/2011 3:50:15 PM
using JavaScript.
It would look something like this. Because javascript does not have date formatting functions:
var d = new Date();
var date = [ d.getDate(), d.getMonth() + 1, d.getFullYear() ];
var time = [
(d.getHours() > 12) ? d.getHours() - 12 : (d.getHours() == 0) ? 12 : d.getHours(),
d.getMinutes(),
d.getSeconds()
];
"".concat(date.join("/"), " ", time.join(":"), " ", (d.getHours() > 11) ? "PM" : "AM");
PS: I hope that the AM/PM is good, I'm not really familiar with it.
Edit: I've checked it's okay now.
You can even use this to extend the Date class in the following way:
Date.prototype.getFormatted = function() {
var date = [ this.getDate(), this.getMonth() + 1, this.getFullYear() ];
var time = [
(this.getHours() > 12) ? this.getHours() - 12 : (this.getHours() == 0) ? 12 : this.getHours(),
this.getMinutes(),
this.getSeconds()
];
return "".concat(date.join("/"), " ", time.join(":"), " ", (this.getHours() > 11) ? "PM" : "AM");
};
and then simply:
var d = new Date();
d.getFormatted();
d.concat, it's just "".concat, so var test = "".concat(...