Here's my (ES5 safe) method to add the YYYYMMDDHHMMSS()
function to any Date
object.
On older browsers, either shim Object.defineProperty
or just add the inner function directly to Date.prototype
:
Object.defineProperty(Date.prototype, 'YYYYMMDDHHMMSS', {
value: function() {
function pad2(n) { // always returns a string
return (n < 10 ? '0' : '') + n;
}
return this.getFullYear() +
pad2(this.getMonth() + 1) +
pad2(this.getDate()) +
pad2(this.getHours()) +
pad2(this.getMinutes()) +
pad2(this.getSeconds());
}
});