如何仅在 JavaScript 中获取当前时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10599148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:04:28  来源:igfitidea点击:

How do I get the current time only in JavaScript

javascriptdatetime

提问by hss

How can I get the current time in JavaScript and use it in a timepicker?

如何在 JavaScript 中获取当前时间并在时间选择器中使用它?

I tried var x = Date()and got:

我尝试var x = Date()并得到:

Tue May 15 2012 05:45:40 GMT-0500

2012 年 5 月 15 日星期二 05:45:40 GMT-0500

But I need only current time, for example, 05:45

但我只需要当前时间,例如, 05:45

How can I assign this to a variable?

如何将其分配给变量?

回答by Royi Namir

var d = new Date("2011-04-20T09:30:51.01");
d.getHours(); // => 9
d.getMinutes(); // =>  30
d.getSeconds(); // => 51

or

或者

var d = new Date(); // for now
d.getHours(); // => 9
d.getMinutes(); // =>  30
d.getSeconds(); // => 51

回答by snir

Short and simple:

简短而简单:

new Date().toLocaleTimeString(); // 11:18:48 AM
//---
new Date().toLocaleDateString(); // 11/16/2015
//---
new Date().toLocaleString(); // 11/16/2015, 11:18:48 PM

4 hours later (use milisec: sec==1000):

4 小时后(使用 milisec:sec==1000):

new Date(new Date().getTime() + 4*60*60*1000).toLocaleTimeString(); // 3:18:48 PM or 15:18:48

2 days before:

2天前:

new Date(new Date().getTime() - 2*24*60*60*1000).toLocaleDateString() // 11/14/2015

回答by DreamTeK

Get and set the current time efficiently using javascript

使用javascript有效地获取和设置当前时间

I couldn't find a solution that did exactly what I needed. I wanted clean and tiny code so I came up with this:

我找不到完全满足我需要的解决方案。我想要干净而小巧的代码,所以我想出了这个:

(Set once in your master.js and invoke when required.)

(在 master.js 中设置一次并在需要时调用。)

PURE JAVASCRIPT

纯Java脚本

function timeNow(i) {
  var d = new Date(),
    h = (d.getHours()<10?'0':'') + d.getHours(),
    m = (d.getMinutes()<10?'0':'') + d.getMinutes();
  i.value = h + ':' + m;
}
<a onclick="timeNow(test1)" href="#">SET TIME</a>
<input id="test1" type="time" value="10:40" />



UPDATE

更新

There is now sufficient browser support to simply use: toLocaleTimeString

现在有足够的浏览器支持来简单地使用: toLocaleTimeString

For html5type timethe format must be hh:mm.

对于html5类型time,格式必须是hh:mm.

function timeNow(i) {
  i.value = new Date().toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
}
<a onclick="timeNow(test1)" href="#">SET TIME</a>
<input id="test1" type="time" value="10:40" />

Try it on jsfiddle

Try it on jsfiddle

回答by Fernando Gomes

Try

尝试

new Date().toLocaleTimeString().replace("/.*(\d{2}:\d{2}:\d{2}).*/", "");

Or

或者

new Date().toTimeString().split(" ")[0];

回答by Sudhir Bastakoti

Do you mean:

你的意思是:

var d = new Date();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();

回答by Parmeet Singh

const date = Date().slice(16,21);
console.log(date);

回答by StoicFnord

Try this:

尝试这个:

var date = new Date();
var hour = date.getHours();
var min = date.getMinutes();

回答by Hamfri

function getCurrentTime(){
    var date = new Date();
    var hh = date.getHours();
    var mm = date.getMinutes();

    hh = hh < 10 ? '0'+hh : hh; 
    mm = mm < 10 ? '0'+mm : mm;

    curr_time = hh+':'+mm;
    return curr_time;
}

回答by Parvez Rahaman

This how you can do it.

这是你如何做到的。

const date = new Date();
const time = date.toTimeString().split(' ')[0].split(':');
console.log(time[0] + ':' + time[1])