This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function padLeft(nr, n, str) { | |
return Array(n - String(nr).length + 1).join(str || '0') + nr; | |
} | |
if (!Date.prototype.toMyString) { | |
Date.prototype.toMyString = function() { | |
var y = this.getFullYear(); | |
var m = this.getMonth() + 1; | |
var d = this.getDate(); | |
return y + '/' + padLeft(m,2) + '/' + padLeft(d,2); | |
} | |
} | |
console.log(new Date('2012/02/05').toMyString()); // 2012/02/05 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Date.prototype.addHours = function(h) { | |
this.setHours(this.getHours() + h); | |
return this; | |
}; | |
Date.prototype.toMyDate = function() { | |
return this.getFullYear() + padLeft(this.getMonth()+1, 2) + padLeft(this.getDate(), 2); | |
}; | |
Date.prototype.toMyTime = function() { | |
return padLeft(this.getHours(), 2) + ':' + padLeft(this.getMinutes(), 2) + ':' + padLeft(this.getSeconds(), 2); | |
}; | |
function padLeft(nr, n, str) { | |
return Array(n - String(nr).length + 1).join(str || '0') + nr; | |
} | |
var now = new Date(), | |
yesterday = new Date().addHours(-24); | |
console.log('now time: %s',now.toMyTime()); | |
console.log('now date: %s',now.toMyDate()); | |
console.log('yesterday time: %s',yesterday.toMyTime()); | |
console.log('yesterday date: %s',yesterday.toMyDate()); |