从UTC日期开始获得工作日的最可靠的方法是什么

发布于 2025-02-11 18:21:15 字数 1938 浏览 1 评论 0原文

这就是我要获得日期的工作日的方式

const date = new Date('2022-06-25T'+hour+':00:00.951Z')
const day1 = date.toLocaleString('en-US', { weekday: 'short' }) 

,这就是我正在构建日期的方式

`${date.getUTCDate()} ${date.toLocaleString('en-GB', { month: 'short' })} ${date.getUTCFullYear()}`

,但显然在某些小时内发生了一些日期(这是我发送的屏幕截图)

如果您注意到6月25日和6月26日实际上是星期六和周日。 两个日期/天都不同步,

因此,在网上搜索后,

const weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
const d = new Date();
let day = weekdays[d.getUTCDay()];

我发现了此实现(使用UTC函数) ,我进行了此小脚本以比较6月25日的结果

const weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']

for (i = 0; i < 24; i++) {
   const hour = i < 10 ? '0' + i : i;
   const date = new Date('2022-06-25T'+hour+':00:00.951Z')
   const day1 = date.toLocaleString('en-US', { weekday: 'short', timeZone: 'Asia/Tbilisi' }) 
   const day2 = weekdays[date.getUTCDay()]
   console.log('2022-06-25 at', hour, day1, day2)
}

您可以在20小时后没有返回的DevTools中看到,

2022-06-25 at 00 Sat Sat
2022-06-25 at 01 Sat Sat
2022-06-25 at 02 Sat Sat
2022-06-25 at 03 Sat Sat
2022-06-25 at 04 Sat Sat
2022-06-25 at 05 Sat Sat
2022-06-25 at 06 Sat Sat
2022-06-25 at 07 Sat Sat
2022-06-25 at 08 Sat Sat
2022-06-25 at 09 Sat Sat
2022-06-25 at 10 Sat Sat
2022-06-25 at 11 Sat Sat
2022-06-25 at 12 Sat Sat
2022-06-25 at 13 Sat Sat
2022-06-25 at 14 Sat Sat
2022-06-25 at 15 Sat Sat
2022-06-25 at 16 Sat Sat
2022-06-25 at 17 Sat Sat
2022-06-25 at 18 Sat Sat
2022-06-25 at 19 Sat Sat
2022-06-25 at 20 Sun Sat
2022-06-25 at 21 Sun Sat
2022-06-25 at 22 Sun Sat
2022-06-25 at 23 Sun Sat

因此WAT是获取工作日名称的最可靠的方式?

This is how I'm getting the date's week day

const date = new Date('2022-06-25T'+hour+':00:00.951Z')
const day1 = date.toLocaleString('en-US', { weekday: 'short' }) 

And this is how I'm constructing the date

`${date.getUTCDate()} ${date.toLocaleString('en-GB', { month: 'short' })} ${date.getUTCFullYear()}`

But apparently fails for some dates in certain hours (this is the screenshot i was sent)
enter image description here

If you notice the 25 of June and the 26 of June are actually Saturday and Sunday. So both date/day are not synced

After searching a bit online I found this implementation (that uses UTC function)

const weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
const d = new Date();
let day = weekdays[d.getUTCDay()];

And I Did this little script to compare the result for 25th of June

const weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']

for (i = 0; i < 24; i++) {
   const hour = i < 10 ? '0' + i : i;
   const date = new Date('2022-06-25T'+hour+':00:00.951Z')
   const day1 = date.toLocaleString('en-US', { weekday: 'short', timeZone: 'Asia/Tbilisi' }) 
   const day2 = weekdays[date.getUTCDay()]
   console.log('2022-06-25 at', hour, day1, day2)
}

And you can see in the devtools that is not returning same day after 20h

2022-06-25 at 00 Sat Sat
2022-06-25 at 01 Sat Sat
2022-06-25 at 02 Sat Sat
2022-06-25 at 03 Sat Sat
2022-06-25 at 04 Sat Sat
2022-06-25 at 05 Sat Sat
2022-06-25 at 06 Sat Sat
2022-06-25 at 07 Sat Sat
2022-06-25 at 08 Sat Sat
2022-06-25 at 09 Sat Sat
2022-06-25 at 10 Sat Sat
2022-06-25 at 11 Sat Sat
2022-06-25 at 12 Sat Sat
2022-06-25 at 13 Sat Sat
2022-06-25 at 14 Sat Sat
2022-06-25 at 15 Sat Sat
2022-06-25 at 16 Sat Sat
2022-06-25 at 17 Sat Sat
2022-06-25 at 18 Sat Sat
2022-06-25 at 19 Sat Sat
2022-06-25 at 20 Sun Sat
2022-06-25 at 21 Sun Sat
2022-06-25 at 22 Sun Sat
2022-06-25 at 23 Sun Sat

So wat's the most reliable way to get the week day name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

<逆流佳人身旁 2025-02-18 18:21:15
 新日期('2022-06-25T'+小时+':00:00.951Z')
 

以UTC的形式解析(提供的小时被填充到2位数字),到目前为止还不错。

  date.toleocalestring('en-us',{工作日:'short',timezone:'asia/tbilisi'})
 

在全年+4的亚洲/第比利斯(Asia/tbilisi)中获取本地日名。因此,对于20:00之后的UTC时代,当地一天将是第二天。要获取UTC日,请在选项中使用时区:'utc',例如

// Current UTC day
console.log(new Date().toLocaleString('en',{weekday: 'short', timeZone: 'UTC'}));

请注意,UTC不是一个时区,实际上是相反的,它是一个标准,它定义了一个基准,从该基准中测量了所有偏移(以及因此时区)。

new Date('2022-06-25T'+hour+':00:00.951Z')

Is parsed as UTC (provided hour is padded to 2 digits), so far so good.

date.toLocaleString('en-US', { weekday: 'short', timeZone: 'Asia/Tbilisi' })

Gets the local day name in Asia/Tbilisi, which is +4 all year round. So for UTC times after 20:00, the local day will be the next day. To get the UTC day, use timeZone: 'UTC' in the options, e.g.

// Current UTC day
console.log(new Date().toLocaleString('en',{weekday: 'short', timeZone: 'UTC'}));

Note that UTC isn't a timezone, it's actually the opposite, it's a standard that defines a datum from which all offsets (and hence timezones) are measured.

南薇 2025-02-18 18:21:15
const date = new Date(Date.UTC(2022, 5, 25, 3, 23, 16, 738));
const weekday = new Intl.DateTimeFormat('en-GB', {  weekday: 'short', timeZone: 'UTC' }).format(date)

console.log(weekday)

const date = new Date(Date.UTC(2022, 5, 25, 3, 23, 16, 738));
const weekday = new Intl.DateTimeFormat('en-GB', {  weekday: 'short', timeZone: 'UTC' }).format(date)

console.log(weekday)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文