在非本地时区(在本例中为太平洋标准时间)查找下一次出现的时间

Find next occurrence of a time in a non-local timezone (in this case, PST)

我想知道下一次特定 PST 时间有多远,而不管客户的时区如何。

如果时间是 UTC,这将是微不足道的,但我不知道如何在 PST 中做到这一点,同时记住夏令时的遵守。

例如。太平洋标准时间下午 4 点将是世界标准时间晚上 11 点,因为现在是夏天。

我不想手动输入夏令时的日期。

如果没有图书馆就不可能,我很乐意使用图书馆。

// returns the number of milliseconds from the current time until the specified time in PST.
function getTimeUntil (hour, minutes = 0, seconds = 0)
{
    // implementation needed
}

以下解释了为什么这可能是 How to initialize a JavaScript Date to a particular time zone 的重复项。

PST(大概是美国太平洋标准时间)是一个具有固定偏移量的时区,UTC -8。遵守太平洋标准时间并实行夏令时的地方通常称该偏移量为太平洋夏令时 (PDT),即 UTC -7。

PST 也可能是皮特凯恩标准时间,它也是 UTC -8,在皮特凯恩岛上全年观察。将 PST 转换为 UTC 是通过增加 8 小时来实现的。

但是,您可能希望使用某个地方的时间和日期,该地点在冬季遵守美国 PST,在夏季遵守美国 PDT,例如洛杉矶。在这种情况下,您可以使用像 Luxon 或 date.js 这样的库,它允许根据时间戳和指定的 IANA 代表位置(例如“America/Los_Angeles”创建日期)。如果是这样,那就看上面的link。

我的实现:

// 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>