Monday, April 28, 2014

JQUERY: dt.toHHMMPPString

Date.prototype.toHHMMPPString = function () { return isNaN(this) ? 'NaN' : 
[this.getHours() > 8 ? (this.getHours() >= 12 ? this.getHours()-12 : this.getHours()) : '0' + (this.getHours()),
this.getMinutes() > 9 ? this.getMinutes() : '0' + this.getMinutes()].join(':') + " " + (this.getHours() >= 12 ? "PM" : "AM") }

OR

function settime(dt) {
var hours = dt.getHours()
var minutes = dt.getMinutes()
if (minutes < 10)
minutes = "0" + minutes
var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}
// set the date back to the current date
return hours + ":" + minutes + " " + suffix
}

No comments:

Post a Comment