0

I want to get how far away is the next occurence of a particular PST time regardless of the client's timezone.

This would be trivial if the time were in UTC but I don't know how to do it in PST keeping in mind the observance of daylight savings time.

Eg. 4 PM PST would be 11 PM UTC since it is right now summer.

I would prefer not to have to manually input the dates of daylight saving time.

I am happy to use a library if this is not possible without one.

// returns the number of milliseconds from the current time until the specified time in PST.
function getTimeUntil (hour, minutes = 0, seconds = 0)
{
    // implementation needed
}
trinalbadger587
  • 1,905
  • 1
  • 18
  • 36
  • Assuming PST is US Pacific Standard Time, then 4 pm (16:00) PST is 00:00 UTC always, since PST has a fixed offset: UTC -8. Most (though not all) places that observe PST in winter observe PDT (UTC -7) in summer. In those places 16:00 PDT is 23:00 UTC. – RobG Jul 17 '21 at 10:49

2 Answers2

1

The following is an explanation of why this is likely a duplicate of How to initialize a JavaScript Date to a particular time zone.

PST (presumably US Pacific Standard Time) is a timezone with a fixed offset, UTC -8. Places that observe PST and have daylight saving typically call that offset Pacific Daylight Time (PDT), which is UTC -7.

PST might also be Pitcairn Standard Time, which is also UTC -8 and observed all year round on Pitcairn Island. Converting PST to UTC is achieved by adding 8 hours.

However, likely you want to work with times and dates for a place that observes US PST in winter and US PDT in summer, e.g. Los Angeles. In that case you can use a library like Luxon or date.js that allows creating dates based on a timestamp and specified IANA representative location such as "America/Los_Angeles". If that is the case, then see the link above.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • I'm not sure where on pacific standard time daylight savings time is not considered but I'm pretty sure that in Canada and the US it is. – trinalbadger587 Jul 17 '21 at 16:24
0

My implementation:

// returns the formatted time from the current time until the specified time in PST.
function getTimeUntil (hour, minutes = 0, seconds = 0)
{
    let future = luxon.DateTime.now().setZone('America/Vancouver').set({
        hours: hour,
        minutes: minutes,
        seconds: seconds
    });
    let now = luxon.DateTime.now().setZone('America/Vancouver');
    if (future < now)
    {
        future = future.plus({ days:1 });
    }
    return future.diff(now, ["hours", "minutes", "seconds"]);
    // implementation needed
}

console.log(getTimeUntil(13, 0, 0).toObject());
<script src="https://cdn.jsdelivr.net/npm/luxon@2.0.1/build/global/luxon.min.js"></script>
trinalbadger587
  • 1,905
  • 1
  • 18
  • 36