夏令时切换日不进行一小时整体转换的时区

问题描述

我们在多伦多时间有一个数据集,想要将其转换为UTC,在11月1日,时间从夏季更改为冬季。

数据集包含日期和一天中的小时。在11月1日,它的小时数正好是24行(0-23)。

问题在于它将第一小时和第二小时都转换为5 AM UTC,然后继续7 AM UTC(跳过6 AM)。

这是应该如何工作的,但是在此用例中,我们需要一整天的数据,而不必在UTC中将两小时变成一小时。

0 - Sun,01 Nov 2020 04:00:00 GMT
1 - Sun,01 Nov 2020 05:00:00 GMT <<-
2 - Sun,01 Nov 2020 07:00:00 GMT <<-
3 - Sun,01 Nov 2020 08:00:00 GMT
4 - Sun,01 Nov 2020 09:00:00 GMT
5 - Sun,01 Nov 2020 10:00:00 GMT

有什么办法可以做到这一点?

https://jsfiddle.net/6peLc79u/1/

const date = "2020-11-01";

for (let i = 0; i < 24; i++) {
  const utcString = moment
    .tz(`${date} ${i}`,"YYYY-MM-DD H","America/Toronto")
    .toDate()
    .toUTCString();
  console.log(i,utcString);
  document.write(`<li>${i} - ${utcString}</li>`)
}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script type="text/javascript" src="https://momentjs.com/downloads/moment-timezone-with-data-10-year-range.min.js"></script>

解决方法

const date = new Date().getTime();//"2020-11-01";
//honestly,a whole module just for dates.. just use plain javascript
for (let i = 0; i < 24; i++) {
  const utcString = new Date(date+(i*3600000)).toGMTString()
  //now in the loop I just added 1 hr for each time i got higher
  console.log(i,utcString);
  document.write(`<li>${i} - ${utcString}</li>`)
}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script type="text/javascript" src="https://momentjs.com/downloads/moment-timezone-with-data-10-year-range.min.js"></script>

,

您是否尝试过更改方式并转换日期和时间以查看是否不是即时错误? 看一下下面链接中提到的一些示例: How to initialize a JavaScript Date to a particular time zone

,

不太确定要从哪里开始和结束,但是这是11月1日,从00:00:00到23:00:00,没有任何框架

options = {
  year: 'numeric',month: 'numeric',day: 'numeric',weekday: 'short',hour: 'numeric',minute: 'numeric',second: 'numeric',timeZone: 'America/Toronto' 
};
//Sun,01 Nov 2020 04:00:00 GMT
const date = new Date(2020,10,1,15,0)
const formatter = new Intl.DateTimeFormat('en-CA',options)
const fmtDateParts = formatter.formatToParts(date);


const weekday = fmtDateParts.find(item => type="weekday").value.replace(".",",")
const times = [];
for (let i=0;i<24;i++) {
  times.push(
   `${weekday} Nov ${date.getFullYear()} ${String(i).padStart("0",2)}:00:00 GMT`
  );
}

console.log(times)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...