Last active
December 18, 2015 03:38
-
-
Save kevinmarx/5719249 to your computer and use it in GitHub Desktop.
Some nice methods to add on to the js date interface
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.stdTimeOffset = function() { | |
var jan = new Date(this.getFullYear(), 0, 1) // july and jan are currently in/out of DST in every timezone. | |
var jul = new Date(this.getFullYear(), 6, 1) | |
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset()) | |
} | |
Date.prototype.isDST = function() { | |
return this.getTimezoneOffset() < this.stdTimeOffset() | |
} | |
Date.prototype.getTimezone = function() { | |
var str = this.toString() | |
var tz = str.match(/\([A-Z]+\)/g)[0].replace(/\(|\)/g, '') // look for the timezone name in the datestring | |
var tzo = this.getTimezoneOffset() // minutes for some reason | |
var neg = str.match(/-\d{4}/g) ? -1 : 1 | |
return { timezone: tz, offset: neg * (tzo * 60000) } | |
} | |
Date.prototype.setTimezone = function(tz) { | |
var ct = this.timezoneOffsets[tz] | |
if (this.currentOffset !== ct) this.setTime(+this + ct) | |
this.currentOffset = ct | |
return this | |
} | |
Date.prototype.timezoneOffsets = { | |
"Africa/Algiers" : 3600000, | |
"Africa/Cairo" : 7200000, | |
"Africa/Casablanca" : 0, | |
"Africa/Harare" : 7200000, | |
"Africa/Johannesburg" : 7200000, | |
"Africa/Monrovia" : 0, | |
"Africa/Nairobi" : 10800000, | |
"America/Argentina/Buenos_Aires" : 10800000, | |
"America/Bogota" : -18000000, | |
"America/Caracas" : -16200000, | |
"America/Chicago" : -21600000, | |
"America/Chihuahua" : -25200000, | |
"America/Denver" : -25200000, | |
"America/Godthab" : 10800000, | |
"America/Guatemala" : -21600000, | |
"America/Guyana" : -14400000, | |
"America/Halifax" : -14400000, | |
"America/Indiana/Indianapolis" : -18000000, | |
"America/Juneau" : -32400000, | |
"America/La_Paz" : -14400000, | |
"America/Lima" : -18000000, | |
"America/Los_Angeles" : -28800000, | |
"America/Mazatlan" : -25200000, | |
"America/Mexico_City" : -21600000, | |
"America/Monterrey" : -21600000, | |
"America/New_York" : -18000000, | |
"America/Phoenix" : -25200000, | |
"America/Regina" : -21600000, | |
"America/Santiago" : -14400000, | |
"America/Sao_Paulo" : 10800000, | |
"America/St_Johns" : -12600000, | |
"America/Tijuana" : -28800000, | |
"Asia/Almaty" : 21600000, | |
"Asia/Baghdad" : 10800000, | |
"Asia/Baku" : 14400000, | |
"Asia/Bangkok" : 25200000, | |
"Asia/Chongqing" : 28800000, | |
"Asia/Colombo" : 19800000, | |
"Asia/Dhaka" : 21600000, | |
"Asia/Hong_Kong" : 28800000, | |
"Asia/Irkutsk" : 32400000, | |
"Asia/Jakarta" : 25200000, | |
"Asia/Jerusalem" : 7200000, | |
"Asia/Kabul" : 16200000, | |
"Asia/Kamchatka" : 43200000, | |
"Asia/Karachi" : 18000000, | |
"Asia/Kathmandu" : 20700000, | |
"Asia/Kolkata" : 19800000, | |
"Asia/Krasnoyarsk" : 28800000, | |
"Asia/Kuala_Lumpur" : 28800000, | |
"Asia/Kuwait" : 10800000, | |
"Asia/Magadan" : 43200000, | |
"Asia/Muscat" : 14400000, | |
"Asia/Novosibirsk" : 25200000, | |
"Asia/Rangoon" : 23400000, | |
"Asia/Riyadh" : 10800000, | |
"Asia/Seoul" : 32400000, | |
"Asia/Shanghai" : 28800000, | |
"Asia/Singapore" : 28800000, | |
"Asia/Taipei" : 28800000, | |
"Asia/Tashkent" : 18000000, | |
"Asia/Tbilisi" : 14400000, | |
"Asia/Tehran" : 12600000, | |
"Asia/Tokyo" : 32400000, | |
"Asia/Ulaanbaatar" : 28800000, | |
"Asia/Urumqi" : 28800000, | |
"Asia/Vladivostok" : 39600000, | |
"Asia/Yakutsk" : 36000000, | |
"Asia/Yekaterinburg" : 21600000, | |
"Asia/Yerevan" : 14400000, | |
"Atlantic/Azores" : -3600000, | |
"Atlantic/Cape_Verde" : -3600000, | |
"Atlantic/South_Georgia" : -7200000, | |
"Australia/Adelaide" : 34200000, | |
"Australia/Brisbane" : 36000000, | |
"Australia/Darwin" : 34200000, | |
"Australia/Hobart" : 36000000, | |
"Australia/Melbourne" : 36000000, | |
"Australia/Perth" : 28800000, | |
"Australia/Sydney" : 36000000, | |
"Europe/Amsterdam" : 3600000, | |
"Europe/Athens" : 7200000, | |
"Europe/Belgrade" : 3600000, | |
"Europe/Berlin" : 3600000, | |
"Europe/Bratislava" : 3600000, | |
"Europe/Brussels" : 3600000, | |
"Europe/Bucharest" : 7200000, | |
"Europe/Budapest" : 3600000, | |
"Europe/Copenhagen" : 3600000, | |
"Europe/Dublin" : 0, | |
"Europe/Helsinki" : 7200000, | |
"Europe/Istanbul" : 7200000, | |
"Europe/Kiev" : 7200000, | |
"Europe/Lisbon" : 0, | |
"Europe/Ljubljana" : 3600000, | |
"Europe/London" : 0, | |
"Europe/Madrid" : 3600000, | |
"Europe/Minsk" : 10800000, | |
"Europe/Moscow" : 14400000, | |
"Europe/Paris" : 3600000, | |
"Europe/Prague" : 3600000, | |
"Europe/Riga" : 7200000, | |
"Europe/Rome" : 3600000, | |
"Europe/Sarajevo" : 3600000, | |
"Europe/Skopje" : 3600000, | |
"Europe/Sofia" : 7200000, | |
"Europe/Stockholm" : 3600000, | |
"Europe/Tallinn" : 7200000, | |
"Europe/Vienna" : 3600000, | |
"Europe/Vilnius" : 7200000, | |
"Europe/Warsaw" : 3600000, | |
"Europe/Zagreb" : 3600000, | |
"Pacific/Apia" : 46800000, | |
"Pacific/Auckland" : 43200000, | |
"Pacific/Fakaofo" : 46800000, | |
"Pacific/Fiji" : 43200000, | |
"Pacific/Guadalcanal" : 39600000, | |
"Pacific/Guam" : 36000000, | |
"Pacific/Honolulu" : -36000000, | |
"Pacific/Majuro" : 43200000, | |
"Pacific/Midway" : -39600000, | |
"Pacific/Noumea" : 39600000, | |
"Pacific/Pago_Pago" : -39600000, | |
"Pacific/Port_Moresby" : 36000000, | |
"Pacific/Tongatapu" : 46800000 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment