将 UTC 时间字符串转换为 javascript 中的本地时区

Convert UTC time string to local timezone in javascript

如何将 UTC 格式的字符串(如“11:00”)转换为相同格式的本地时间。我在 javascript 中使用 Date 函数来转换它,但我想要一个快速可靠的替代方法。

还可以获取当地时区的时区缩写和星期几。

How to convert a string in UTC like '11:00' to local time in the same format.

在下文中,"local" 表示非 UTC 方法返回的值,它考虑了主机系统时区设置。

一种方法是创建一个日期,将UTC时间设置为合适的时间,然后读回本地时间值,例如

var d = new Date();
// Set time to 11:00
d.setUTCHours(11,0,0,0);
// Get "local" date and time
console.log(d.toString());

I have used Date function in javascript to convert that but I want a fast and reliable alternative.

要求推荐书籍、图书馆、工具等的问题是题外话。

Also to get Time zone abbreviation

有多种资源可以估计时区的名称,但是没有标准的名称。大多数解析方法都是基于不同日期的主机时区偏移量,这不是 100% 可靠的,特别是因为没有就名称应该是什么达成一致。

and day of the week in the local timezone.

本地星期几由 getDay 方法提供,因此:

var dayNumber = d.getDay(); // 0 for Sunday, 1 for Monday, etc.

如果支持 ECMA-402 国际化接口,您可以使用主机默认语言获取工作日名称:

var d = new Date();
console.log('Today is ' + d.toLocaleString(undefined, {weekday: 'long'}) + '.')