首页 > 解决方案 > 从 - new Date().getTime() 获取时间;

问题描述

我正在尝试获取设备的时间。我正在使用的代码是这样的 -

   const estimatedDeviceTimeMs = new Date().getTime();
    alert(new Date(estimatedDeviceTimeMs));

上面代码的结果是Thu Jul 30 2020 12:01:54 GMT+0530 (India Standard Time)

如果可能,我需要取出12:01:54一个变量和GMT+0530另一个变量。

标签: javascript

解决方案


toTimeString返回时间。然后按空间拆分以获得时间和时区,如下所示:

var date = new Date();
console.log(date.toTimeString().split(" ")[0]);
console.log(date.toTimeString().split(" ")[1]);


推荐阅读