0

we have a dataset in Toronto Time and want to convert it to UTC, on the first of November, the time was changed from summer to winter time there.

The dataset contains the date and the hour of the day. On November the 1st it has exactly 24 rows, (0-23) for the hour.

The issue is that it converts both the hour 1 and 2 into 5 AM UTC and then continues with 7 AM UTC (skipping 6 AM).

This is how it is supposed to work, but in this use-case, we need the full day of data without it moving two hours into one hour in 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

Is there any way to achieve this?

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>
user2693017
  • 1,750
  • 6
  • 24
  • 50
  • Now that the data have been recorded, there is no way of knowing whether a specific record was recorded during the actual 5AM hour or during the second 5AM hour. That's why it's important to record the offset along with the UTC time. – Heretic Monkey Nov 03 '20 at 18:43

3 Answers3

0

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>
The Bomb Squad
  • 4,192
  • 1
  • 9
  • 17
0

Have you tried to change the way and convert the dates and times to see if it is not a moment bug? Take a look at some examples mentioned in the link below: How to initialize a JavaScript Date to a particular time zone

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

Not quite sure where you want to start and end, but here is Nov 1st from 00:00:00 to 23:00:00 without any framework

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,0,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)
mplungjan
  • 169,008
  • 28
  • 173
  • 236