gpt4 book ai didi

javascript - 如何将 Etc/GMT+2 时区的时间戳转换为 unix 时间戳?

转载 作者:太空宇宙 更新时间:2023-11-04 00:35:35 24 4
gpt4 key购买 nike

我需要转换以下日期时间格式:

Start: {
DateTime: '2016-09-01T03:00:00.0000000',
TimeZone: 'Etc/GMT+2'
},

转换为 unix 时间戳 1472691600000

即使输入时区发生变化,此转换也应该有效。

这是我通过 Office365 API 获取的数据。在 Office365 日历(在浏览器中)中,我将事件设置为 8:00 UTC+2 开始。

我一段时间以来一直在尝试寻找此问题的解决方案,因此我们将不胜感激。

最佳答案

时间戳是有效的 ISO 8601 日期时间,因此我们可以简单地使用 javascript 的 Date.parse() 将其解析为 unix 时间戳。

但是,时区似乎是 POSIX 时区。正如 this StackOverflow answer 所说,Etc/GMT+2 应转换为 GMT-2(ISO 8601 中的 -0200),而不是 +2

如果时间戳可以更改,则意味着我们应该有一个 list of all valid timestamps 。我编写了一个简单的解析器来获取 JS 可解析格式的时间戳:

find ./* -exec grep ^Zone {} \; | awk '{print $2" "$3}' | awk -F '[ :]' '{ printf "\"%s\": \"%+03.0f%02.0f\"\n", $1, $2, $3 }'

这会输出格式化的时区,我们可以将其用作 JS 数组。这样我们就可以将 POSIX 时区转换为 ISO 8601 时区格式。

最终的代码是:

function convertToTimestamp(datetimeObject) {

var timezones = {
"Africa/Algiers": "+0012",
"Atlantic/Cape_Verde": "-0134",
"Africa/Ndjamena": "+0100",
"Africa/Abidjan": "-0016",
"Africa/Cairo": "+0205",
"Africa/Accra": "-0000",
"Africa/Bissau": "-0102",
"Africa/Nairobi": "+0227",
"Africa/Monrovia": "-0043",
"Africa/Tripoli": "+0052",
"Indian/Mauritius": "+0350",
"Africa/Casablanca": "-0030",
"Africa/El_Aaiun": "-0052",
"Africa/Maputo": "+0210",
"Africa/Windhoek": "+0108",
"Africa/Lagos": "+0013",
"Indian/Reunion": "+0341",
"Indian/Mahe": "+0341",
"Africa/Johannesburg": "+0152",
"Africa/Khartoum": "+0210",
"Africa/Tunis": "+0040",
"Antarctica/Casey": "+0000",
"Antarctica/Davis": "+0000",
"Antarctica/Mawson": "+0000",
"Indian/Kerguelen": "+0000",
"Antarctica/DumontDUrville": "+0000",
"Antarctica/Syowa": "+0000",
"Antarctica/Troll": "+0000",
"Antarctica/Vostok": "+0000",
"Antarctica/Rothera": "+0000",
"Asia/Kabul": "+0436",
"Asia/Yerevan": "+0258",
"Asia/Baku": "+0319",
"Asia/Dhaka": "+0601",
"Asia/Thimphu": "+0558",
"Indian/Chagos": "+0449",
"Asia/Brunei": "+0739",
"Asia/Rangoon": "+0624",
"Asia/Shanghai": "+0805",
"Asia/Urumqi": "+0550",
"Asia/Hong_Kong": "+0736",
"Asia/Taipei": "+0806",
"Asia/Macau": "+0734",
"Asia/Nicosia": "+0213",
"Asia/Tbilisi": "+0259",
"Asia/Dili": "+0822",
"Asia/Kolkata": "+0553",
"Asia/Jakarta": "+0707",
"Asia/Pontianak": "+0717",
"Asia/Makassar": "+0757",
"Asia/Jayapura": "+0922",
"Asia/Tehran": "+0325",
"Asia/Baghdad": "+0257",
"Asia/Jerusalem": "+0220",
"Asia/Tokyo": "+0918",
"Asia/Amman": "+0223",
"Asia/Almaty": "+0507",
"Asia/Qyzylorda": "+0421",
"Asia/Aqtobe": "+0348",
"Asia/Aqtau": "+0321",
"Asia/Oral": "+0325",
"Asia/Bishkek": "+0458",
"Asia/Seoul": "+0827",
"Asia/Pyongyang": "+0823",
"Asia/Beirut": "+0222",
"Asia/Kuala_Lumpur": "+0646",
"Asia/Kuching": "+0721",
"Indian/Maldives": "+0454",
"Asia/Hovd": "+0606",
"Asia/Ulaanbaatar": "+0707",
"Asia/Choibalsan": "+0738",
"Asia/Kathmandu": "+0541",
"Asia/Karachi": "+0428",
"Asia/Gaza": "+0217",
"Asia/Hebron": "+0220",
"Asia/Manila": "-1556",
"Asia/Qatar": "+0326",
"Asia/Riyadh": "+0306",
"Asia/Singapore": "+0655",
"Asia/Colombo": "+0519",
"Asia/Damascus": "+0225",
"Asia/Dushanbe": "+0435",
"Asia/Bangkok": "+0642",
"Asia/Ashgabat": "+0353",
"Asia/Dubai": "+0341",
"Asia/Samarkand": "+0427",
"Asia/Tashkent": "+0437",
"Asia/Ho_Chi_Minh": "+0706",
"Australia/Darwin": "+0843",
"Australia/Perth": "+0743",
"Australia/Eucla": "+0835",
"Australia/Brisbane": "+1012",
"Australia/Lindeman": "+0955",
"Australia/Adelaide": "+0914",
"Australia/Hobart": "+0949",
"Australia/Currie": "+0935",
"Australia/Melbourne": "+0939",
"Australia/Sydney": "+1004",
"Australia/Broken_Hill": "+0925",
"Australia/Lord_Howe": "+1036",
"Antarctica/Macquarie": "+0000",
"Indian/Christmas": "+0702",
"Indian/Cocos": "+0627",
"Pacific/Fiji": "+1155",
"Pacific/Gambier": "-0859",
"Pacific/Marquesas": "-0918",
"Pacific/Tahiti": "-0958",
"Pacific/Guam": "-1421",
"Pacific/Tarawa": "+1132",
"Pacific/Enderbury": "-1124",
"Pacific/Kiritimati": "-1029",
"Pacific/Majuro": "+1124",
"Pacific/Kwajalein": "+1109",
"Pacific/Chuuk": "+1007",
"Pacific/Pohnpei": "+1032",
"Pacific/Kosrae": "+1051",
"Pacific/Nauru": "+1107",
"Pacific/Noumea": "+1105",
"Pacific/Auckland": "+1139",
"Pacific/Chatham": "+1213",
"Pacific/Rarotonga": "-1039",
"Pacific/Niue": "-1119",
"Pacific/Norfolk": "+1111",
"Pacific/Palau": "+0857",
"Pacific/Port_Moresby": "+0948",
"Pacific/Bougainville": "+1022",
"Pacific/Pitcairn": "-0840",
"Pacific/Pago_Pago": "+1237",
"Pacific/Apia": "+1233",
"Pacific/Guadalcanal": "+1039",
"Pacific/Fakaofo": "-1124",
"Pacific/Tongatapu": "+1219",
"Pacific/Funafuti": "+1156",
"Pacific/Wake": "+1106",
"Pacific/Efate": "+1113",
"Pacific/Wallis": "+1215",
"Africa/Addis_Ababa": "+0234",
"Africa/Asmara": "+0235",
"Africa/Bamako": "-0032",
"Africa/Bangui": "+0114",
"Africa/Banjul": "-0106",
"Africa/Blantyre": "+0220",
"Africa/Brazzaville": "+0101",
"Africa/Bujumbura": "+0157",
"Africa/Conakry": "-0054",
"Africa/Dakar": "-0109",
"Africa/Dar_es_Salaam": "+0237",
"Africa/Djibouti": "+0252",
"Africa/Douala": "+0038",
"Africa/Freetown": "-0053",
"Africa/Gaborone": "+0143",
"Africa/Harare": "+0204",
"Africa/Juba": "+0206",
"Africa/Kampala": "+0209",
"Africa/Kigali": "+0200",
"Africa/Kinshasa": "+0101",
"Africa/Libreville": "+0037",
"Africa/Lome": "+0004",
"Africa/Luanda": "+0052",
"Africa/Lubumbashi": "+0149",
"Africa/Lusaka": "+0153",
"Africa/Malabo": "+0035",
"Africa/Maseru": "+0150",
"Africa/Mbabane": "+0204",
"Africa/Mogadishu": "+0301",
"Africa/Niamey": "+0008",
"Africa/Nouakchott": "-0103",
"Africa/Ouagadougou": "-0006",
"Africa/Porto-Novo": "+0010",
"Africa/Sao_Tome": "+0026",
"Africa/Timbuktu": "-0012",
"America/Anguilla": "-0412",
"America/Antigua": "-0407",
"America/Argentina/ComodRivadavia": "-0430",
"America/Aruba": "-0440",
"America/Cayman": "-0525",
"America/Coral_Harbour": "-0532",
"America/Dominica": "-0405",
"America/Ensenada": "-0746",
"America/Grenada": "-0407",
"America/Guadeloupe": "-0406",
"America/Montreal": "-0454",
"America/Montserrat": "-0408",
"America/Rosario": "-0402",
"America/St_Kitts": "-0410",
"America/St_Lucia": "-0404",
"America/St_Thomas": "-0419",
"America/St_Vincent": "-0404",
"America/Tortola": "-0418",
"Antarctica/McMurdo": "+0000",
"Asia/Aden": "+0259",
"Asia/Bahrain": "+0322",
"Asia/Chongqing": "+0706",
"Asia/Hanoi": "+0703",
"Asia/Harbin": "+0826",
"Asia/Kashgar": "+0503",
"Asia/Kuwait": "+0311",
"Asia/Muscat": "+0354",
"Asia/Phnom_Penh": "+0659",
"Asia/Tel_Aviv": "+0219",
"Asia/Vientiane": "+0650",
"Atlantic/Jan_Mayen": "-0100",
"Atlantic/St_Helena": "-0022",
"Europe/Belfast": "-0023",
"Europe/Guernsey": "-0009",
"Europe/Isle_of_Man": "-0017",
"Europe/Jersey": "-0008",
"Europe/Ljubljana": "+0058",
"Europe/Sarajevo": "+0113",
"Europe/Skopje": "+0125",
"Europe/Tiraspol": "+0158",
"Europe/Vaduz": "+0038",
"Europe/Zagreb": "+0103",
"Indian/Antananarivo": "+0310",
"Indian/Comoro": "+0253",
"Indian/Mayotte": "+0300",
"Pacific/Johnston": "-1000",
"Pacific/Midway": "-1149",
"Pacific/Saipan": "-1417",
"Etc/GMT": "+0000",
"Etc/UTC": "+0000",
"Etc/UCT": "+0000",
"Etc/GMT-14": "+1400",
"Etc/GMT-13": "+1300",
"Etc/GMT-12": "+1200",
"Etc/GMT-11": "+1100",
"Etc/GMT-10": "+1000",
"Etc/GMT-9": "+0900",
"Etc/GMT-8": "+0800",
"Etc/GMT-7": "+0700",
"Etc/GMT-6": "+0600",
"Etc/GMT-5": "+0500",
"Etc/GMT-4": "+0400",
"Etc/GMT-3": "+0300",
"Etc/GMT-2": "+0200",
"Etc/GMT-1": "+0100",
"Etc/GMT+1": "-0100",
"Etc/GMT+2": "-0200",
"Etc/GMT+3": "-0300",
"Etc/GMT+4": "-0400",
"Etc/GMT+5": "-0500",
"Etc/GMT+6": "-0600",
"Etc/GMT+7": "-0700",
"Etc/GMT+8": "-0800",
"Etc/GMT+9": "-0900",
"Etc/GMT+10": "-1000",
"Etc/GMT+11": "-1100",
"Etc/GMT+12": "-1200",
"Europe/London": "-0001",
"Europe/Dublin": "-0025",
"WET": "+0000",
"CET": "+0100",
"MET": "+0100",
"EET": "+0200",
"Europe/Tirane": "+0119",
"Europe/Andorra": "+0006",
"Europe/Vienna": "+0105",
"Europe/Minsk": "+0150",
"Europe/Brussels": "+0017",
"Europe/Sofia": "+0133",
"Europe/Prague": "+0057",
"Europe/Copenhagen": "+0050",
"Atlantic/Faroe": "-0027",
"America/Danmarkshavn": "-0114",
"America/Scoresbysund": "-0127",
"America/Godthab": "-0326",
"America/Thule": "-0435",
"Europe/Tallinn": "+0139",
"Europe/Helsinki": "+0139",
"Europe/Paris": "+0009",
"Europe/Berlin": "+0053",
"Europe/Gibraltar": "-0021",
"Europe/Athens": "+0134",
"Europe/Budapest": "+0116",
"Atlantic/Reykjavik": "-0128",
"Europe/Rome": "+0049",
"Europe/Riga": "+0136",
"Europe/Vilnius": "+0141",
"Europe/Luxembourg": "+0024",
"Europe/Malta": "+0058",
"Europe/Chisinau": "+0155",
"Europe/Monaco": "+0029",
"Europe/Amsterdam": "+0019",
"Europe/Oslo": "+0043",
"Europe/Warsaw": "+0124",
"Europe/Lisbon": "-0036",
"Atlantic/Azores": "-0142",
"Atlantic/Madeira": "-0107",
"Europe/Bucharest": "+0144",
"Europe/Kaliningrad": "+0122",
"Europe/Moscow": "+0230",
"Europe/Simferopol": "+0216",
"Europe/Astrakhan": "+0312",
"Europe/Volgograd": "+0257",
"Europe/Kirov": "+0318",
"Europe/Samara": "+0320",
"Europe/Ulyanovsk": "+0313",
"Asia/Yekaterinburg": "+0402",
"Asia/Omsk": "+0453",
"Asia/Barnaul": "+0535",
"Asia/Novosibirsk": "+0531",
"Asia/Tomsk": "+0539",
"Asia/Novokuznetsk": "+0548",
"Asia/Krasnoyarsk": "+0611",
"Asia/Irkutsk": "+0657",
"Asia/Chita": "+0733",
"Asia/Yakutsk": "+0838",
"Asia/Vladivostok": "+0847",
"Asia/Khandyga": "+0902",
"Asia/Sakhalin": "+0930",
"Asia/Magadan": "+1003",
"Asia/Srednekolymsk": "+1014",
"Asia/Ust-Nera": "+0932",
"Asia/Kamchatka": "+1034",
"Asia/Anadyr": "+1149",
"Europe/Belgrade": "+0122",
"Europe/Madrid": "-0014",
"Africa/Ceuta": "-0021",
"Atlantic/Canary": "-0101",
"Europe/Stockholm": "+0112",
"Europe/Zurich": "+0034",
"Europe/Istanbul": "+0155",
"Europe/Kiev": "+0202",
"Europe/Uzhgorod": "+0129",
"Europe/Zaporozhye": "+0220",
"Factory": "+0000",
"EST": "-0500",
"MST": "-0700",
"HST": "-1000",
"EST5EDT": "-0500",
"CST6CDT": "-0600",
"MST7MDT": "-0700",
"PST8PDT": "-0800",
"America/New_York": "-0456",
"America/Chicago": "-0550",
"America/North_Dakota/Center": "-0645",
"America/North_Dakota/New_Salem": "-0645",
"America/North_Dakota/Beulah": "-0647",
"America/Denver": "-0659",
"America/Los_Angeles": "-0752",
"America/Juneau": "+1502",
"America/Sitka": "+1458",
"America/Metlakatla": "+1513",
"America/Yakutat": "+1441",
"America/Anchorage": "+1400",
"America/Nome": "+1258",
"America/Adak": "+1213",
"Pacific/Honolulu": "-1031",
"America/Phoenix": "-0728",
"America/Boise": "-0744",
"America/Indiana/Indianapolis": "-0544",
"America/Indiana/Marengo": "-0545",
"America/Indiana/Vincennes": "-0550",
"America/Indiana/Tell_City": "-0547",
"America/Indiana/Petersburg": "-0549",
"America/Indiana/Knox": "-0546",
"America/Indiana/Winamac": "-0546",
"America/Indiana/Vevay": "-0540",
"America/Kentucky/Louisville": "-0543",
"America/Kentucky/Monticello": "-0539",
"America/Detroit": "-0532",
"America/Menominee": "-0550",
"America/St_Johns": "-0330",
"America/Goose_Bay": "-0401",
"America/Halifax": "-0414",
"America/Glace_Bay": "-0359",
"America/Moncton": "-0419",
"America/Blanc-Sablon": "-0348",
"America/Toronto": "-0517",
"America/Thunder_Bay": "-0557",
"America/Nipigon": "-0553",
"America/Rainy_River": "-0618",
"America/Atikokan": "-0606",
"America/Winnipeg": "-0628",
"America/Regina": "-0658",
"America/Swift_Current": "-0711",
"America/Edmonton": "-0733",
"America/Vancouver": "-0812",
"America/Dawson_Creek": "-0800",
"America/Fort_Nelson": "-0810",
"America/Creston": "-0746",
"America/Pangnirtung": "+0000",
"America/Iqaluit": "+0000",
"America/Resolute": "+0000",
"America/Rankin_Inlet": "+0000",
"America/Cambridge_Bay": "+0000",
"America/Yellowknife": "+0000",
"America/Inuvik": "+0000",
"America/Whitehorse": "-0900",
"America/Dawson": "-0917",
"America/Cancun": "-0547",
"America/Merida": "-0558",
"America/Matamoros": "-0640",
"America/Monterrey": "-0641",
"America/Mexico_City": "-0636",
"America/Ojinaga": "-0657",
"America/Chihuahua": "-0704",
"America/Hermosillo": "-0723",
"America/Mazatlan": "-0705",
"America/Bahia_Banderas": "-0701",
"America/Tijuana": "-0748",
"America/Nassau": "-0509",
"America/Barbados": "-0358",
"America/Belize": "-0552",
"Atlantic/Bermuda": "-0419",
"America/Costa_Rica": "-0536",
"America/Havana": "-0529",
"America/Santo_Domingo": "-0439",
"America/El_Salvador": "-0556",
"America/Guatemala": "-0602",
"America/Port-au-Prince": "-0449",
"America/Tegucigalpa": "-0548",
"America/Jamaica": "-0507",
"America/Martinique": "-0404",
"America/Managua": "-0545",
"America/Panama": "-0518",
"America/Puerto_Rico": "-0424",
"America/Miquelon": "-0344",
"America/Grand_Turk": "-0444",
"America/Argentina/Buenos_Aires": "-0353",
"America/Argentina/Cordoba": "-0416",
"America/Argentina/Salta": "-0421",
"America/Argentina/Tucuman": "-0420",
"America/Argentina/La_Rioja": "-0427",
"America/Argentina/San_Juan": "-0434",
"America/Argentina/Jujuy": "-0421",
"America/Argentina/Catamarca": "-0423",
"America/Argentina/Mendoza": "-0435",
"America/Argentina/San_Luis": "-0425",
"America/Argentina/Rio_Gallegos": "-0436",
"America/Argentina/Ushuaia": "-0433",
"America/La_Paz": "-0432",
"America/Noronha": "-0209",
"America/Belem": "-0313",
"America/Santarem": "-0338",
"America/Fortaleza": "-0234",
"America/Recife": "-0219",
"America/Araguaina": "-0312",
"America/Maceio": "-0222",
"America/Bahia": "-0234",
"America/Sao_Paulo": "-0306",
"America/Campo_Grande": "-0338",
"America/Cuiaba": "-0344",
"America/Porto_Velho": "-0415",
"America/Boa_Vista": "-0402",
"America/Manaus": "-0400",
"America/Eirunepe": "-0439",
"America/Rio_Branco": "-0431",
"America/Santiago": "-0442",
"Pacific/Easter": "-0717",
"Antarctica/Palmer": "+0000",
"America/Bogota": "-0456",
"America/Curacao": "-0435",
"America/Guayaquil": "-0519",
"Pacific/Galapagos": "-0558",
"Atlantic/Stanley": "-0351",
"America/Cayenne": "-0329",
"America/Guyana": "-0352",
"America/Asuncion": "-0350",
"America/Lima": "-0508",
"Atlantic/South_Georgia": "-0226",
"America/Paramaribo": "-0340",
"America/Port_of_Spain": "-0406",
"America/Montevideo": "-0344",
"America/Caracas": "-0427"
};

var isoDateFormat = datetimeObject.Start.DateTime + timezones[datetimeObject.Start.TimeZone];
return Date.parse(isoDateFormat);
}

您可以将其用于:

var timestampObject = {
Start: {
DateTime: '2016-09-01T03:00:00.0000000',
TimeZone: 'Etc/GMT+2'
}
};
convertToTimestamp(timestampObject); // Outputs 1472706000000

关于javascript - 如何将 Etc/GMT+2 时区的时间戳转换为 unix 时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39193590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com