Created
June 17, 2015 05:16
-
-
Save sstur/03c9e7b384da37e9025b to your computer and use it in GitHub Desktop.
UTCDate as a subclass of Date
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 UTCDate(a) { | |
var x; | |
switch (arguments.length) { | |
case 0: | |
x = new Date(); | |
break; | |
case 1: | |
x = (typeof a === 'number') ? new Date(a) : new Date(Date.parse(a)); | |
break; | |
default: | |
x = new Date(Date.UTC.apply(Date, arguments)); | |
} | |
x.__proto__ = UTCDate.prototype; | |
return x; | |
} | |
UTCDate.prototype.__proto__ = Date.prototype; | |
UTCDate.prototype.getDate = Date.prototype.getUTCDate; | |
UTCDate.prototype.getDay = Date.prototype.getUTCDay; | |
UTCDate.prototype.getFullYear = Date.prototype.getUTCFullYear; | |
UTCDate.prototype.getHours = Date.prototype.getUTCHours; | |
UTCDate.prototype.getMilliseconds = Date.prototype.getUTCMilliseconds; | |
UTCDate.prototype.getMinutes = Date.prototype.getUTCMinutes; | |
UTCDate.prototype.getMonth = Date.prototype.getUTCMonth; | |
UTCDate.prototype.getSeconds = Date.prototype.getUTCSeconds; | |
UTCDate.prototype.setDate = Date.prototype.setUTCDate; | |
UTCDate.prototype.setFullYear = Date.prototype.setUTCFullYear; | |
UTCDate.prototype.setHours = Date.prototype.setUTCHours; | |
UTCDate.prototype.setMilliseconds = Date.prototype.setUTCMilliseconds; | |
UTCDate.prototype.setMinutes = Date.prototype.setUTCMinutes; | |
UTCDate.prototype.setMonth = Date.prototype.setUTCMonth; | |
UTCDate.prototype.setSeconds = Date.prototype.setUTCSeconds; | |
UTCDate.prototype.toString = Date.prototype.toUTCString; | |
UTCDate.prototype.toDateString = function() { | |
var parts = this.toUTCString().split(' '); | |
return parts[0].slice(0, -1) + ' ' + parts[2] + ' ' + parts[1] + ' ' + parts[3]; | |
}; | |
UTCDate.prototype.toTimeString = function() { | |
var parts = this.toUTCString().split(' '); | |
return parts[4] + ' ' + parts[5]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment