【问题标题】:How can I get the epoch time for a specific time of day in a specific timezone?如何获取特定时区中特定时间的纪元时间?
【发布时间】:2020-11-21 00:01:11
【问题描述】:

我正在尝试获取特定时区中当天的纪元时间戳。

例如,我想知道太平洋标准时间今天早上 6 点的纪元时间戳

我已经很接近了,但不确定如何调整时区:

const getTimestampFor10amPST = () => {
  const today = new Date()
  const year = today.getFullYear()
  const month = today.getMonth()
  const day = today.getDate()
  const timestampFor10amPST = new Date(year, month, day, 10, 0, 0, 0);

  return timestampFor10amPST.getTime()
}

如何获取预期时区的时间戳?

【问题讨论】:

    标签: javascript date timezone


    【解决方案1】:

    您可以使用Luxon

    const dt = luxon.DateTime.fromObject({hour: 10, zone: 'America/Los_Angeles'});
    
    console.log('Time in time zone: ' + dt.toString());
    console.log('Unix (epoch) timestamp in milliseconds: ' + dt.toMillis());
    <script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.25.0/luxon.min.js"></script>

    这是有效的,因为当您不提供日期组件时,Luxon 会使用提供的时区中的当前日期作为基础。此外,当您提供任何时间组件时,剩余时间组件将设置为零。因此,您只需设置小时和时区。

    【讨论】:

    • 请记住,您的当前日期可能与给定时区的当前日期不同。 Luxon 正确地解释了这一点。
    • 另外值得注意的是,'America/Los_Angeles' 将适用于洛杉矶的历史偏移和 DST 规则,它不会专门为始终为 -8 的 PST(可能是美国太平洋标准时间或皮特凯恩标准时间)为此,真的不需要图书馆。 ;-)
    • 确实,我假设太平洋标准时间,OP 实际上是指太平洋时间,包括 PST (-8) 或 PDT (-7),具体取决于在给定时间生效的时间。如果您实际上想要一个固定的 -8 偏移量,那么您可以在没有 Luxon 的情况下执行此操作。
    【解决方案2】:

    试试这样的:

    function calcTime(city, offset) {
    
        d = new Date();
    
        utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    
        nd = new Date(utc + (3600000*offset));
    
        return "The local time in " + city + " is " + nd.toLocaleString();
    
    }
    
    // get Bombay time
    console.log(calcTime('Bombay', '+5.5'));
    
    // get Singapore time
    console.log(calcTime('Singapore', '+8'));
    
    // get London time
    console.log(calcTime('London', '+1'));
    

    另一种方法是使用选项对象,因为它有一个 timeZoneName 参数

    var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
    
    // an application may want to use UTC and make that visible
    var options = { timeZone: 'UTC', timeZoneName: 'short' };
    console.log(date.toLocaleTimeString('en-US', options));
    // → "3:00:00 AM GMT"
    
    // sometimes even the US needs 24-hour time
    console.log(date.toLocaleTimeString('en-US', { hour12: false }));
    // → "19:00:00"
    
    // show only hours and minutes, use options with the default locale - use an empty array
    console.log(date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }));
    // → "20:01"
    

    如果您希望它返回日期对象而不是字符串,则以下解决方案将适合您:

    var d = new Date();
    var date_object = new Date(formatDate(d.toLocaleString('en-US', { 'timeZone': 'America/New_York', 'hour12': false })));
    function formatDate(date){
        var date = date.split(', ')[0].split('/');
        var time = date.split(', ')[1].split(':');
        return date[2]+'-'+date[0]+'-'+date[1]+'T'+time[0]+':'+time[1]+':'+time[2];
    }
    

    【讨论】:

    • 嗨。在您的第一种方法中,您混淆了偏移的时区。例如,伦敦并不总是 +1,有时是 +0。第二个示例根本没有解决所提出的问题,因为它返回一个字符串。
    • 嗨。对不起,不行。那仍然行不通。一个常见的误解是您可以操纵Date 对象的时区,但它不能可靠地完成。您在这里所做的是在给定的时区格式化日期字符串,然后通过将其传递给 new Date 您将其解释为好像它位于用户的本地时区 - 而不是提供的时区。 Date 对象不能处于给定时区。它只是一个基于 UTC 的 Unix 时间戳的薄包装,具有一些与本地时间相互转换的功能。
    • 另外,您在原始问题中错过了要求 Unix 时间戳(又名“epoch timstamp”)的内容。你的代码没有给出。
    猜你喜欢
    • 2016-07-25
    • 1970-01-01
    • 2016-03-01
    • 2021-09-22
    • 2015-03-25
    • 2018-10-27
    • 1970-01-01
    • 2012-01-02
    • 2016-08-23
    相关资源
    最近更新 更多