【问题标题】:How to safely extract time from date time - Mon Aug 05 2019 14:29:16 GMT+0530 (India Standard Time) [duplicate]如何安全地从日期时间中提取时间 - 2019 年 8 月 5 日星期一 14:29:16 GMT+0530(印度标准时间)[重复]
【发布时间】:2019-08-13 17:07:34
【问题描述】:

从输入字段我得到这个值

Mon Aug 05 2019 14:29:16 GMT+0530 (India Standard Time)

如何安全地得到这部分14:29:16这是时间

【问题讨论】:

  • 你有什么尝试吗?我们愿意为您提供帮助,但您最好展示您尝试过的方法和无效的方法。

标签: javascript


【解决方案1】:

var date = new Date("Mon Aug 05 2019 14:29:16 GMT+0530 (India Standard Time)");
document.getElementById("time").innerHTML = date.getHours() + " : " + date.getMinutes() + " : " + date.getSeconds();
<!DOCTYPE html>
<html>

<body>
The content of the body element is displayed in your browser.
<h1 id="time"></h1>
</body>

</html>

【讨论】:

    【解决方案2】:

    要获得确切的解决方案,这将起作用:

    function getTime(date) {
      const hours = date.getHours();
      const minutes = date.getMinutes();
      const seconds = date.getSeconds();
      return [hours, minutes, seconds].join(':');
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      • 2018-12-01
      • 2020-09-14
      • 1970-01-01
      相关资源
      最近更新 更多