Convert a date (in UTC) to date in another timezone (with support for DST)
问题陈述 - 我需要将UTC格式的日期转换为另一个时区的日期。 其他时区可能支持也可能不支持夏令时。
数据可供我使用:
使用上述数据,我可以将时间转换为所需的时区,但如何检查DST当前是否对该时区有效? 如果我知道DST是活动的,那么我可以使用DST偏移而不是标准偏移。
技术 - JavaScript,React-Native(如果需要,我愿意使用moment.js)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | //see https://gist.github.com/danderson00/c03d46dfc38486ea7b06da9f75c7a63f let windowsToIANA = { "Dateline Standard Time":"Etc/GMT+12", "UTC-11":"Etc/GMT+11", "Aleutian Standard Time":"America/Adak", "Hawaiian Standard Time":"Pacific/Honolulu", "Marquesas Standard Time":"Pacific/Marquesas", "Alaskan Standard Time":"America/Anchorage", "UTC-09":"Etc/GMT+9", "Pacific Standard Time (Mexico)":"America/Tijuana", "UTC-08":"Etc/GMT+8", "Pacific Standard Time":"America/Los_Angeles", "US Mountain Standard Time":"America/Phoenix", "Mountain Standard Time (Mexico)":"America/Chihuahua", "Mountain Standard Time":"America/Denver", "Central America Standard Time":"America/Guatemala", "Central Standard Time":"America/Chicago", "Easter Island Standard Time":"Pacific/Easter", "Central Standard Time (Mexico)":"America/Mexico_City", "Canada Central Standard Time":"America/Regina", "SA Pacific Standard Time":"America/Bogota", "Eastern Standard Time (Mexico)":"America/Cancun", "Eastern Standard Time":"America/New_York", "Haiti Standard Time":"America/Port-au-Prince", "Cuba Standard Time":"America/Havana", "US Eastern Standard Time":"America/Indianapolis", "Paraguay Standard Time":"America/Asuncion", "Atlantic Standard Time":"America/Halifax", "Venezuela Standard Time":"America/Caracas", "Central Brazilian Standard Time":"America/Cuiaba", "SA Western Standard Time":"America/La_Paz", "Pacific SA Standard Time":"America/Santiago", "Turks And Caicos Standard Time":"America/Grand_Turk", "Newfoundland Standard Time":"America/St_Johns", "Tocantins Standard Time":"America/Araguaina", "E. South America Standard Time":"America/Sao_Paulo", "SA Eastern Standard Time":"America/Cayenne", "Argentina Standard Time":"America/Buenos_Aires", "Greenland Standard Time":"America/Godthab", "Montevideo Standard Time":"America/Montevideo", "Magallanes Standard Time":"America/Punta_Arenas", "Saint Pierre Standard Time":"America/Miquelon", "Bahia Standard Time":"America/Bahia", "UTC-02":"Etc/GMT+2", "Azores Standard Time":"Atlantic/Azores", "Cape Verde Standard Time":"Atlantic/Cape_Verde", "UTC":"Etc/GMT", "Morocco Standard Time":"Africa/Casablanca", "GMT Standard Time":"Europe/London", "Greenwich Standard Time":"Atlantic/Reykjavik", "W. Europe Standard Time":"Europe/Berlin", "Central Europe Standard Time":"Europe/Budapest", "Romance Standard Time":"Europe/Paris", "Central European Standard Time":"Europe/Warsaw", "W. Central Africa Standard Time":"Africa/Lagos", "Jordan Standard Time":"Asia/Amman", "GTB Standard Time":"Europe/Bucharest", "Middle East Standard Time":"Asia/Beirut", "Egypt Standard Time":"Africa/Cairo", "E. Europe Standard Time":"Europe/Chisinau", "Syria Standard Time":"Asia/Damascus", "West Bank Standard Time":"Asia/Hebron", "South Africa Standard Time":"Africa/Johannesburg", "FLE Standard Time":"Europe/Kiev", "Israel Standard Time":"Asia/Jerusalem", "Kaliningrad Standard Time":"Europe/Kaliningrad", "Sudan Standard Time":"Africa/Khartoum", "Libya Standard Time":"Africa/Tripoli", "Namibia Standard Time":"Africa/Windhoek", "Arabic Standard Time":"Asia/Baghdad", "Turkey Standard Time":"Europe/Istanbul", "Arab Standard Time":"Asia/Riyadh", "Belarus Standard Time":"Europe/Minsk", "Russian Standard Time":"Europe/Moscow", "E. Africa Standard Time":"Africa/Nairobi", "Iran Standard Time":"Asia/Tehran", "Arabian Standard Time":"Asia/Dubai", "Astrakhan Standard Time":"Europe/Astrakhan", "Azerbaijan Standard Time":"Asia/Baku", "Russia Time Zone 3":"Europe/Samara", "Mauritius Standard Time":"Indian/Mauritius", "Saratov Standard Time":"Europe/Saratov", "Georgian Standard Time":"Asia/Tbilisi", "Caucasus Standard Time":"Asia/Yerevan", "Afghanistan Standard Time":"Asia/Kabul", "West Asia Standard Time":"Asia/Tashkent", "Ekaterinburg Standard Time":"Asia/Yekaterinburg", "Pakistan Standard Time":"Asia/Karachi", "India Standard Time":"Asia/Calcutta", "Sri Lanka Standard Time":"Asia/Colombo", "Nepal Standard Time":"Asia/Katmandu", "Central Asia Standard Time":"Asia/Almaty", "Bangladesh Standard Time":"Asia/Dhaka", "Omsk Standard Time":"Asia/Omsk", "Myanmar Standard Time":"Asia/Rangoon", "SE Asia Standard Time":"Asia/Bangkok", "Altai Standard Time":"Asia/Barnaul", "W. Mongolia Standard Time":"Asia/Hovd", "North Asia Standard Time":"Asia/Krasnoyarsk", "N. Central Asia Standard Time":"Asia/Novosibirsk", "Tomsk Standard Time":"Asia/Tomsk", "China Standard Time":"Asia/Shanghai", "North Asia East Standard Time":"Asia/Irkutsk", "Singapore Standard Time":"Asia/Singapore", "W. Australia Standard Time":"Australia/Perth", "Taipei Standard Time":"Asia/Taipei", "Ulaanbaatar Standard Time":"Asia/Ulaanbaatar", "Aus Central W. Standard Time":"Australia/Eucla", "Transbaikal Standard Time":"Asia/Chita", "Tokyo Standard Time":"Asia/Tokyo", "North Korea Standard Time":"Asia/Pyongyang", "Korea Standard Time":"Asia/Seoul", "Yakutsk Standard Time":"Asia/Yakutsk", "Cen. Australia Standard Time":"Australia/Adelaide", "AUS Central Standard Time":"Australia/Darwin", "E. Australia Standard Time":"Australia/Brisbane", "AUS Eastern Standard Time":"Australia/Sydney", "West Pacific Standard Time":"Pacific/Port_Moresby", "Tasmania Standard Time":"Australia/Hobart", "Vladivostok Standard Time":"Asia/Vladivostok", "Lord Howe Standard Time":"Australia/Lord_Howe", "Bougainville Standard Time":"Pacific/Bougainville", "Russia Time Zone 10":"Asia/Srednekolymsk", "Magadan Standard Time":"Asia/Magadan", "Norfolk Standard Time":"Pacific/Norfolk", "Sakhalin Standard Time":"Asia/Sakhalin", "Central Pacific Standard Time":"Pacific/Guadalcanal", "Russia Time Zone 11":"Asia/Kamchatka", "New Zealand Standard Time":"Pacific/Auckland", "UTC+12":"Etc/GMT-12", "Fiji Standard Time":"Pacific/Fiji", "Chatham Islands Standard Time":"Pacific/Chatham", "UTC+13":"Etc/GMT-13", "Tonga Standard Time":"Pacific/Tongatapu", "Samoa Standard Time":"Pacific/Apia", "Line Islands Standard Time":"Pacific/Kiritimati" } //create link Object.entries(windowsToIANA).forEach(([win, IANA]) => { moment.tz.link(`${IANA}|${win}`); }) var newYork = moment.tz("2014-01-01 12:00","Eastern Standard Time"); var london = moment.tz("2014-06-01 12:00","GMT Standard Time"); console.log(newYork.format('ha z')) console.log(newYork.isDST()) console.log(london.format('ha z')) console.log(london.isDST()) |
1 2 | <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.21/moment-timezone-with-data.min.js"> |