pingeyeg
pingeyeg

Reputation: 680

Converting UTC seconds to get hours of operation

I'm not very familiar with UTC, but from what I do understand, it starts at 0 so I'm trying to understand how you wold convert a UTC time of +32400. I've been told +32400 should equate to 9:00am UTC time, but I'm unclear on how that works.

I was trying something like this, but it's giving me, at the moment, 2am.

const dateIn = Date.now() + (day[1].openInSecondsUTC * 1000);
const dateInTimeStamp = new Date(dateIn);

Upvotes: 1

Views: 518

Answers (1)

Randy Casburn
Randy Casburn

Reputation: 14165

You are confounding the idea of UTC (Universal Time Coordinated) which is a common time reference as a time with a Unix time stamp which is a number of seconds.

What you are looking for is a Date object's value as a Unix timestamp (# seconds).

This code produces 9am.

As you see, the starting time is 00:00:00, so taking the unix time stamp (.valueOf()) and adding your seconds, then creating new date from that will produce the output.

const date = new Date(Date.UTC(0, 0, 0, 0, 0, 0));
const nineAM = new Date(date.valueOf() + 32400000);
console.log(nineAM.toUTCString());

Upvotes: 2

Related Questions