我正在开发一个 Chrome 扩展。在控制台中,我显示 URL 和在网站上花费的时间。
但是我想以小时.分钟.秒的格式显示日期...而不是像 5202163202120623136520002145632100 这样的时间格式...我不明白如何使用 getHours() getMinutes()和 getSecondes() 在我的例子中?
我的内容脚本
var lastPing = +new Date();
function ping() {
chrome.runtime.sendMessage({
domain: location.host,
lastPing: lastPing
}, function() {
lastPing = +new Date();
console.log('Ping envoyé');
}); } setInterval(ping, 5000);
我的背景
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
var domain = request.domain;
var lastPing = request.lastPing;
var time_elapsed = localStorage.getItem(domain) || 0;
localStorage.setItem(domain, time_elapsed + (+new Date() - lastPing));
});
最佳答案
获取日期字符串的时间部分:
new Date().toTimeString().substr(0, 8)
关于javascript - 如何在 javascript 中使用 getHours() (在本例中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23382993/