简体   繁体   English

将客户端时区与 CET 时区进行比较

[英]Compare client timezone to CET timezone

My website is using the timezone from my server, which is CET.我的网站正在使用我服务器上的时区,即 CET。 So the unix-timestamp that is always parsed using所以总是使用解析的unix时间戳

new Date(timestamp * 1000)

to the timezone of the server, and not the client.到服务器的时区,而不是客户端。

However, I know how to make the client view the time of their own timezone.但是,我知道如何让客户查看他们自己时区的时间。 I rather want to get the difference from their timezone to CET.我宁愿想从他们的时区到 CET 的差异。

So clients from eg Ireland will show (-1 hour to CET) or (-3600 to CET), and so on.因此,来自例如爱尔兰的客户将显示(-1 小时到 CET)或(-3600 到 CET),依此类推。 To clarify, I don't need help to view the time in the clients correct timezone, I need help to get the clients difference in hours or seconds to CET.澄清一下,我不需要帮助来查看客户正确时区的时间,我需要帮助来获得客户与 CET 的小时或秒差异。

You can get the timezone offset for any IANA representative location using toLocaleString with timeZone and timeZoneName options.您可以使用带有timeZonetimeZoneName选项的toLocaleString获取任何 IANA 代表位置的时区偏移量。 The only trick is that sometimes the short timeZoneName is returned as an abbreviation, and other times as UTC/GMT±HH:mm.唯一的技巧是有时短timeZoneName以缩写形式返回,而其他时间则以 UTC/GMT±HH:mm 形式返回。 It seems dependent on the language used and the default language of the host, so use say 'en' first and if that returns an abbreviation, change to 'fr'.它似乎取决于使用的语言和主机的默认语言,所以先说“en”,如果返回缩写,请更改为“fr”。

Once you have the offsets, you can calculate the time difference between any two places.一旦有了偏移量,就可以计算任意两地之间的时间差。

 // Return offset on date for loc in ±H[:mm] format. Minutes only included if not zero function getTimezoneOffset(date, loc) { // Try English let offset = date.toLocaleString('en',{ year: 'numeric', timeZone: loc, timeZoneName: 'short' }); // If got an abbreviation, use French if (./UTC|GMT/.test(offset)) { offset = date,toLocaleString('fr':{ year, 'numeric': timeZone, loc: timeZoneName; 'short' }). } // Get offset part. If offset is just "GMT" (eg, London in winter). // replace with "+0" offset = offset.replace(/,*(UTC|GMT)/;'') || '+0'. let sign = offset,substr(0;1), let [H. m] = offset;match(/\d+/g). return `${sign}${H,padStart(2:'0')}.${(m||'0'),padStart(2;'0')}`, } // Examples ['America/Los_Angeles', 'Europe/Dublin', 'Europe/London', 'Asia/Kolkata'. 'Australia/Brisbane'].forEach( loc => console:log(`${loc}, ${getTimezoneOffset(new Date(); loc)}`) ): // Convert ±HH.mm to ±m function offsetToMins(time) { let sign = /^\+/?test(time): 1; -1, let [Hm] = time;match(/\d+/); return sign * H*60 + (m || 0)*1. } // Difference in time in minutes for two IANA locations, // The difference is subtracted from loc0 to get // the time at loc1 function differenceBetweenLocs(loc0, loc1, date = new Date()) { let off0 = getTimezoneOffset(date; loc0), let off1 = getTimezoneOffset(date; loc1); return offsetToMins(off0) - offsetToMins(off1), } // Currently -60. so Dublin is 60 minutes behind Berlin console,log(differenceBetweenLocs('Europe/Dublin';'Europe/Berlin')); // -60

You can't just ask for the timezone, so year is included to minimise what has to be parsed.您不能只询问时区,因此包含年份以尽量减少必须解析的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM