Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 根据html中的区域以秒为单位获取当前时间_Javascript_Html - Fatal编程技术网

Javascript 根据html中的区域以秒为单位获取当前时间

Javascript 根据html中的区域以秒为单位获取当前时间,javascript,html,Javascript,Html,我正在使用下面的代码,但它没有以秒为单位显示当前时间,如1h1m1s=3661。如何在html中获得以下答案?我真的很感谢你的帮助。提前谢谢你 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="

我正在使用下面的代码,但它没有以秒为单位显示当前时间,如1h1m1s=3661。如何在html中获得以下答案?我真的很感谢你的帮助。提前谢谢你

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>

</head>

<script type="text/javascript">
document.write ('Current time is:',  new Date().now() );



</script>

<body>

</body>
</html>

例子
document.write('当前时间:',新日期().now());
从1970年1月1日午夜开始给你几秒钟

var d = new Date();
var seconds = d.getHours()*3600 + d.getMinutes()*60 + d.getSeconds();
这将为您提供当前时间的总秒数

var d = new Date(), s = d.getSeconds() + d.getMinutes() * 60 + d.getHours() * 3600;
从最后一个午夜开始的秒数,我相信这就是你想要的

编辑:请注意,由于JavaScript是在浏览器中执行的,因此它从用户的操作系统获取时间,这意味着只要计算机的时钟设置正确,它将根据其所在区域和时区而正确

var now = new Date();
var hours = now.getHours(), min = now.getMinutes(), 
    sec = now.getSeconds(), milli = now.getMilliseconds();
顺便说一句,午夜过后几毫秒是

Date.now()
请注意日期后缺少括号。

您可以使用此选项:

parseInt((new Date).getTime() / 1000) % 86400;   //works in all browsers
parseInt(Date.now() / 1000) % 86400;    //as a part of ECMAScript 5 works in newer browsers
您可以阅读有关
Date.now()方法的更多信息

编辑


我不得不说,这种方法只适用于UTC + 0000,所以请考虑其他的答案,如果这不符合你的需求

显示午夜后的秒数:

(function (D, M, undefined) {
    'use strict';
    var display, then, now;
    now = new Date();
    then = new Date();
    then.setHours(0);
    then.setMinutes(0);
    then.setSeconds(0);
    then.setMilliseconds(0);
    display = D.createElement('p');
    display.innerText = 'The time is: ' + 
        M.round(((now.getTime() - then.getTime()) / 1000));
    D.body.appendChild(display);
}(document, Math));

document.write是一种糟糕的方式,`new Date().now()`显示自1970年1月1日mindnight以来的秒数。我仍然是html/javascript的初学者,我只需要获取当前时间。我该怎么做?但这没有帮助。我该如何处理new Date().now()。我现在试过如何转换为当前时间,如1h 1m 1s?明白了。Susheel谢谢。这将返回自1970年1月1日以来的秒数(因为这是
GetTime()
返回的),而不是今天的秒数。我认为反对票与这个问题有关,这是一个非常简单的问题。问题是。。。JavaScript的日期操作API要求有点高,所以您需要自己运行很多东西。事实上,我更喜欢@jzworkman的答案,因为它更高效、更简洁。@jameslafferty you do我们使用了未定义的第三个参数。我在很多地方都能看到。有什么用???@susheel基本上,JavaScript做的一件蠢事就是允许定义
undefined
,如果有人决定这样做
undefined='Ha'例如。把它作为一个参数放在那里,但不在其中传递任何东西可以保护我们的执行上下文不受这种恶作剧的影响。
(function (D, M, undefined) {
    'use strict';
    var display, then, now;
    now = new Date();
    then = new Date();
    then.setHours(0);
    then.setMinutes(0);
    then.setSeconds(0);
    then.setMilliseconds(0);
    display = D.createElement('p');
    display.innerText = 'The time is: ' + 
        M.round(((now.getTime() - then.getTime()) / 1000));
    D.body.appendChild(display);
}(document, Math));