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

时间:2022-11-26 07:28:19

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, like 05:45

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

How can I assign this to a variable?

如何将其分配给变量?

14 个解决方案

#1


138  

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

#2


50  

Get and set the current time efficiently using 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

纯粹的JAVASCRIPT

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"/>

<br/>
<a onclick="timeNow(test2)" href="#">SET TIME</a>
<input id="test2" type="time" value="10:40"/>

Try it on jsfiddle

在jsfiddle上尝试一下

#3


43  

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

#4


17  

Try

尝试

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

Or

要么

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

#5


7  

Do you mean:

你的意思是:

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

#6


7  

Try this:

尝试这个:

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

#7


4  

this -

这个 -

var x = new Date(); 
var h = x.getHours(); 
var m = x.getMinutes(); 
var s = x.getSeconds(); 

so-

所以-

 x = date
h = hours
m = mins
s = seconds

#8


3  

See these Date methods ...

查看这些日期方法......

#9


2  

var date = Date().slice(16,21);
document.write(date);

#10


2  

Assign to variables and display it.

分配给变量并显示它。

time = new Date();

var hh = time.getHours();
var mm = time.getMinutes();
var ss = time.getSeconds() 

document.getElementById("time").value = hh + ":" + mm + ":" + ss;

#11


1  

This is the shortest way.

这是最短的方式。

var now = new Date().toLocaleTimeString();
console.log(now)

Here is also a way through string manipulation that was not mentioned.

这里还有一种未提及的字符串操作方法。

var now = new Date()
console.log(now.toString().substr(16,8))

#12


1  

This how you can do it.

这是你如何做到的。

var date = new Date();
var time = date.toTimeString().split(' ')[0].split(':');

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

#13


0  

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;
}

#14


-1  

var hours = date.getHours();
        var minutes = date.getMinutes();
        var ampm = hours >= 12 ? 'pm' : 'am';
        hours = hours % 12;
        hours = hours ? hours : 12; // the hour '0' should be '12'
        minutes = minutes < 10 ? '0'+minutes : minutes;
        var strTime = hours + ':' + minutes + ' ' + ampm;
        console.log(strTime);
        $scope.time = strTime;

        date.setDate(date.getDate()+1);
        month = '' + (date.getMonth() + 1),
        day = '' + date.getDate(1),
        year = date.getFullYear();
        if (month.length < 2) month = '0' + month;
        if (day.length < 2) day = '0' + day;
        var tomorrow = [year, month, day].join('-');
        $scope.tomorrow = tomorrow;

#1


138  

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

#2


50  

Get and set the current time efficiently using 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

纯粹的JAVASCRIPT

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"/>

<br/>
<a onclick="timeNow(test2)" href="#">SET TIME</a>
<input id="test2" type="time" value="10:40"/>

Try it on jsfiddle

在jsfiddle上尝试一下

#3


43  

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

#4


17  

Try

尝试

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

Or

要么

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

#5


7  

Do you mean:

你的意思是:

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

#6


7  

Try this:

尝试这个:

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

#7


4  

this -

这个 -

var x = new Date(); 
var h = x.getHours(); 
var m = x.getMinutes(); 
var s = x.getSeconds(); 

so-

所以-

 x = date
h = hours
m = mins
s = seconds

#8


3  

See these Date methods ...

查看这些日期方法......

#9


2  

var date = Date().slice(16,21);
document.write(date);

#10


2  

Assign to variables and display it.

分配给变量并显示它。

time = new Date();

var hh = time.getHours();
var mm = time.getMinutes();
var ss = time.getSeconds() 

document.getElementById("time").value = hh + ":" + mm + ":" + ss;

#11


1  

This is the shortest way.

这是最短的方式。

var now = new Date().toLocaleTimeString();
console.log(now)

Here is also a way through string manipulation that was not mentioned.

这里还有一种未提及的字符串操作方法。

var now = new Date()
console.log(now.toString().substr(16,8))

#12


1  

This how you can do it.

这是你如何做到的。

var date = new Date();
var time = date.toTimeString().split(' ')[0].split(':');

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

#13


0  

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;
}

#14


-1  

var hours = date.getHours();
        var minutes = date.getMinutes();
        var ampm = hours >= 12 ? 'pm' : 'am';
        hours = hours % 12;
        hours = hours ? hours : 12; // the hour '0' should be '12'
        minutes = minutes < 10 ? '0'+minutes : minutes;
        var strTime = hours + ':' + minutes + ' ' + ampm;
        console.log(strTime);
        $scope.time = strTime;

        date.setDate(date.getDate()+1);
        month = '' + (date.getMonth() + 1),
        day = '' + date.getDate(1),
        year = date.getFullYear();
        if (month.length < 2) month = '0' + month;
        if (day.length < 2) day = '0' + day;
        var tomorrow = [year, month, day].join('-');
        $scope.tomorrow = tomorrow;