是否存在以语言环境字符串格式获取UTC日期的函数? [英] Is there a function to get the UTC date in the Locale String Format?

查看:44
本文介绍了是否存在以语言环境字符串格式获取UTC日期的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用JavaScript获取当前UTC日期,但以本地日期格式显示(例如 Date.toLocaleDateString()那样).

我首先尝试使用 Date.toUTCString()获取当前的UTC日期,但是实际上并没有以本地格式打印出来.

然后,我尝试使用 toLocaleDateString()中的选项配置,但是只是以本地格式打印了本地日期而不是UTC日期.例如new Date().toLocaleDateString(options = {timeZone:"UTC"})

然后,我尝试使用 Intl.DateTimeFormat()进行格式化,但是所得到的结果与 Date.toLocaleDateString()相同.

如果有一种获取语言环境格式的方法,那么我很乐意使用该格式来格式化UTC日期,但据我所知没有.

例如,给定 new Date("Sat,30 Mar 2019 00:27:19 GMT"),在美国,我应该在欧洲我应该为每个受支持的语言环境打印出"30/3/2019",依此类推.

但是,新日期("Sat,30 Mar 2019 00:27:19 GMT").toLocaleDateString(options = {timeZone:"UTC"})将打印出"3/29/2019".

解决方案

我还想使用本地化的字符串设置来显示日期,例如 toLocaleDateString()那样,但是要使用日期的UTC值.本地时区.

例如:

我确实想要本地化的字符串格式,但是我也想要UTC值而不是本地时区值.在此示例中,所需的输出将是 4/3/2019 ,而不是 4/2/2019 .


我承认@AndersonH​​appens的建议,但是我没有使用它.这是我所做的,而不是:

有一个

作为功能的解决方案可能是这样的:

  function toLocaleUTCDateString(date,locales,options){const timeDiff = date.getTimezoneOffset()* 60000;const AdjustedDate =新的Date(date.valueOf()+ timeDiff);返回AdjustedDate.toLocaleDateString(语言环境,选项);} 

I want to get the current UTC date in JavaScript, but display it in the local date format (like Date.toLocaleDateString() does).

I first tried to just get the current UTC Date with Date.toUTCString() but that doesn't actually print out in the local format.

I then tried using the options configuration in toLocaleDateString(), but that just printed the local date and not the UTC date in the local format. e.g. new Date().toLocaleDateString(options = {timeZone: "UTC"})

I then tried formatting using Intl.DateTimeFormat(), but that just gives the same results as Date.toLocaleDateString() does.

If there was a way to get the locale format then I'd be happy to use that format to format the UTC Date, but as far as I can tell there is none.

For example, given the new Date("Sat, 30 Mar 2019 00:27:19 GMT"), In the US, I should print out "3/30/2019", in Europe I should print out "30/3/2019", and so on, for every supported locale.

However, new Date("Sat, 30 Mar 2019 00:27:19 GMT").toLocaleDateString(options = {timeZone: "UTC"}) will print out "3/29/2019" instead.

解决方案

I also wanted to display a date using localized string settings, like toLocaleDateString() does, but using the date's UTC value, instead of the local time zone.

For example:

I do want the localized string format, but I also want the UTC value instead of the local time zone value. The desired output in this example would be 4/3/2019, instead of 4/2/2019.


I acknowledged @AndersonHappens suggestion, but I have not used it. Here is what I did, instead:

There is a getTimezoneOffset() function, which provides the local time zone offset.

We can use this function result and create a new Date, applying the diff. Then, we can use toLocaleDateString() directly, as usual, to get the date in localized string format:

The solution as a function could be like this:

function toLocaleUTCDateString(date, locales, options) {
    const timeDiff = date.getTimezoneOffset() * 60000;
    const adjustedDate = new Date(date.valueOf() + timeDiff);
    return adjustedDate.toLocaleDateString(locales, options);
}

这篇关于是否存在以语言环境字符串格式获取UTC日期的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆