【问题标题】:How to get hours and minutes from UTC string?如何从 UTC 字符串中获取小时和分钟?
【发布时间】:2019-09-26 05:08:11
【问题描述】:

我想将日期时间从本地时间转换为 UTC,然后将该 UTC 时间解码回我原来的本地时间。

为了将本地时间转换为 UTC,我使用了以下代码,效果很好。

const now = new Date();    
let d = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), 13, 5, 0);
console.log(new Date(d).getHours(), new Date(d).getMinutes());

以上代码提供的 UTC 时间为 +5:30,输出为 18,35 (18:35:00)

在这里,为了将 18,35 (18:35:00) 转换回 13:05:00,我使用了以下代码

const now = new Date();
let d = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 18, 35, 0);
console.log(d.toUTCString());

上面的代码为我提供了一个字符串格式的日期,它的时间是 13:05:00,这是正确的。

现在,我想从这个日期字符串中获取小时和分钟。

我已尝试添加:

new Date(d.UTCString()) 

但是,它为我提供了 18 小时和 35 分钟,而我想要 13 小时和 05 分钟。 请帮我解决一下这个。谢谢。

【问题讨论】:

  • 试试这个代码 -> console.log(d.getUTCHours(), d.getUTCMinutes());
  • UTC 不是一种格式。

标签: javascript date


【解决方案1】:

你似乎把事情复杂化了。要获取 UTC 时间,只需使用 getUTC 方法获取小时和分钟值。

要获取 UTC 时间的等效本地时间,请使用 setUTC 方法设置小时和分钟值,例如

// Helper to pad single digits
function z(n) {
  return ('0' + n).slice(-2);
}

let d = new Date();
let localTime = z(d.getHours()) + ':' + z(d.getMinutes());
let utcTime = z(d.getUTCHours()) + ':' + z(d.getUTCMinutes());

// Print local time for d
console.log('Local time: ' + localTime);

// Print UTC time for d
console.log('UTC time: ' + utcTime);

// Set the time using UTC time in HH:mm format
utcTime = "13:05";
let [utcH, utcM] = utcTime.split(':');
d.setUTCHours(utcH, utcM);

console.log('At UTC ' + utcTime + ' the local time is ' + z(d.getHours()) + ':' + z(d.getMinutes()));

【讨论】:

    【解决方案2】:

    您可以使用 getTimezoneOffset 。这是获取当前 UTC 小时和分钟的示例

    var curLocalDate = new Date();
    var curlLocalMiliSec = curLocalDate.getTime();
    var utcOffsetInMin   = curLocalDate.getTimezoneOffset();
    var utcOffsetInMiliSec = utcOffsetInMin * 60 * 1000;
    
    var utcTime = new Date(curlLocalMiliSec + utcOffsetInMiliSec);
    
    var utcHour = utcTime.getHours();
    var utcMinutes = utcTime.getMinutes();
    
    console.log("Local time:" + curLocalDate.getHours() + ":" + curLocalDate.getMinutes());
    
    console.log("UTC time:" + utcHour + ":" + utcMinutes);

    【讨论】:

    • 不需要那么多麻烦,getUTCHoursgetUTCMInutes 等直接提供 UTC 等效项。偏移偏移量可能会将日期移动到夏令时边界之外,因此在明显随机和极少数情况下时间可能是错误的。
    • @RobG,您已经可以在 getTimezoneOffset 文档中找到:“返回的时区偏移量适用于调用它的日期。如果主机系统配置为夏令时,则偏移量将根据 Date 表示的日期和时间以及适用夏令时而改变。”,它负责夏令时。
    • 但是在我描述的情况下,您不希望它为您处理它。假设您在 UTC +6,因此减去 6 小时。但这会使您进入夏令时,其中偏移量为 +7,因此实际上从本地时间中减去了 5 小时。
    【解决方案3】:

    const now = new Date();
    let d = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 18, 35, 0);
    console.log(d.getUTCHours(), d.getUTCMinutes());

    【讨论】:

    • d的目的是什么?你可以做now.setHours(18, 35, 0)并在console.log部分使用now
    【解决方案4】:

    解决方案:

    1. 使用setHours()setMinutes() JS 日期函数设置小时和分钟
    2. 使用 getHours()getMintues() JS 日期函数获取小时和分钟

    注意:现在日期以用户当前时间为准。

    const now = new Date();
    now.setHours(15);
    now.setMinutes(30);
    
    console.log(now.toString());
    console.log(now.getMinutes());
    console.log(now.getMinutes());

    了解时间的资源:@​​987654321@

    【讨论】:

    • 我的问题是如何从我从 d.toUTCString() 方法获得的 UTC 字符串中获取时间和分钟,如果我使用 new Date() 它将转换回我原来的小时和分钟传递给转换
    【解决方案5】:

    这里是toLocaleTimeString方法

    const now = new Date();    
    let d = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), 13, 5, 0);
    
    console.log('locale', new Date(d).toLocaleTimeString('ru-kz', { timeStyle: 'short' }))
    console.log('utc', new Date(d).getUTCHours() + ':' + new Date(d).getUTCMinutes())

    【讨论】:

    • 他希望 13 为小时,05 为分钟
    • @Avi 现在可以了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多