如何获得当前的时间与jQuery

以下内容返回以微秒为单位的时间,例如4565212462。

alert( $.now() ); 

如何将其转换为人类可读的时间格式,例如(小时:分钟:秒)

你可以试试这样的:

 new Date($.now()); 

也使用Javascript你可以这样做:

 var dt = new Date(); var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds(); 

您需要手动提取所有“号码”

喜欢这个:

 var currentdate = new Date(); var datetime = "Now: " + currentdate.getDate() + "/" + (currentdate.getMonth()+1) + "/" + currentdate.getFullYear() + " @ " + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds(); document.write(datetime); 

使用Date.prototype的转换Date.prototype器之一将Date对象转换为string,例如:

 var d = new Date(); d+''; // "Sun Dec 08 2013 18:55:38 GMT+0100" d.toDateString(); // "Sun Dec 08 2013" d.toISOString(); // "2013-12-08T17:55:38.130Z" d.toLocaleDateString() // "8/12/2013" on my system d.toLocaleString() // "8/12/2013 18.55.38" on my system d.toUTCString() // "Sun, 08 Dec 2013 17:55:38 GMT" 

或者,如果你想更自定义,请参阅Date.prototype的getter方法列表 。

你不需要为此使用jQuery!

本地JavaScript实现是Date.now()

Date.now()$.now()返回相同的值:

 Date.now(); // 1421715573651 $.now(); // 1421715573651 
 new Date(Date.now()) // Mon Jan 19 2015 20:02:55 GMT-0500 (Eastern Standard Time) new Date($.now()); // Mon Jan 19 2015 20:02:55 GMT-0500 (Eastern Standard Time) 

..如果你想在hh-mm-ss格式化的时间:

 var now = new Date(Date.now()); var formatted = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds(); // 20:10:58 
 .clock { width: 260px; margin: 0 auto; padding: 30px; color: #FFF;background:#333; } .clock ul { width: 250px; margin: 0 auto; padding: 0; list-style: none; text-align: center } .clock ul li { display: inline; font-size: 3em; text-align: center; font-family: "Arial", Helvetica, sans-serif; text-shadow: 0 2px 5px #55c6ff, 0 3px 6px #55c6ff, 0 4px 7px #55c6ff } #Date { font-family: 'Arial', Helvetica, sans-serif; font-size: 26px; text-align: center; text-shadow: 0 2px 5px #55c6ff, 0 3px 6px #55c6ff; padding-bottom: 40px; } #point { position: relative; -moz-animation: mymove 1s ease infinite; -webkit-animation: mymove 1s ease infinite; padding-left: 10px; padding-right: 10px } /* Animasi Detik Kedap - Kedip */ @-webkit-keyframes mymove { 0% {opacity:1.0; text-shadow:0 0 20px #00c6ff;} 50% {opacity:0; text-shadow:none; } 100% {opacity:1.0; text-shadow:0 0 20px #00c6ff; } } @-moz-keyframes mymove { 0% {opacity:1.0; text-shadow:0 0 20px #00c6ff;} 50% {opacity:0; text-shadow:none; } 100% {opacity:1.0; text-shadow:0 0 20px #00c6ff; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { // Making 2 variable month and day var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; var dayNames= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"] // make single object var newDate = new Date(); // make current time newDate.setDate(newDate.getDate()); // setting date and time $('#Date').html(dayNames[newDate.getDay()] + " " + newDate.getDate() + ' ' + monthNames[newDate.getMonth()] + ' ' + newDate.getFullYear()); setInterval( function() { // Create a newDate() object and extract the seconds of the current time on the visitor's var seconds = new Date().getSeconds(); // Add a leading zero to seconds value $("#sec").html(( seconds < 10 ? "0" : "" ) + seconds); },1000); setInterval( function() { // Create a newDate() object and extract the minutes of the current time on the visitor's var minutes = new Date().getMinutes(); // Add a leading zero to the minutes value $("#min").html(( minutes < 10 ? "0" : "" ) + minutes); },1000); setInterval( function() { // Create a newDate() object and extract the hours of the current time on the visitor's var hours = new Date().getHours(); // Add a leading zero to the hours value $("#hours").html(( hours < 10 ? "0" : "" ) + hours); }, 1000); }); </script> <div class="clock"> <div id="Date"></div> <ul> <li id="hours"></li> <li id="point">:</li> <li id="min"></li> <li id="point">:</li> <li id="sec"></li> </ul> </div> 

jQuery.now()返回:数字

说明:返回代表当前时间的数字。

此方法不接受任何参数。

$.now()方法是由expression式(new Date).getTime()返回的数字的简写。

http://api.jquery.com/jQuery.now/

使用Javascript很简单:

 var d = new Date(); var time = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); console.log(time); 

jQuery的$ .now()是一个新的Date()。getTime(),一个内部的Javascript函数的别名。

http://api.jquery.com/jquery.now/

这将返回自1970年以来经过的秒数,通常称为(不一定正确)为Unix时间,时代或时间戳,取决于您所处的圆圈。使用简单的math计算date/时间之间的差异可能非常方便。 它没有任何时区信息,始终是UTC。

http://en.wikipedia.org/wiki/Unix_time

除了这个别名,没有必要使用jQuery,它没有什么别的帮助date/时间操作。

如果您正在寻找以文本方式表示时间的快速且肮脏的方法,则JavaScript Date对象具有“toString”原型,该原型将返回ISO格式的Date Time。

 new Date().toString(); //returns "Thu Apr 30 2015 14:37:36 GMT+0100 (BST)" 

不过,您可能想要自定义格式。 Date对象有能力提取出相关的细节,所以你可以build立你自己的string表示。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

 var d = new Date(); //without params it defaults to "now" var t = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); //Will return 14:37:36 

然而,正如你所要求的一个jQuery解决scheme – 或许你正在使用旧的浏览器。 如果你想做更具体的事情 – 特别是将string解释成Date对象(对API响应有用),你可能想看看Moment.js。

http://momentjs.com/

这将确保跨浏览器的兼容性,并有很多改进的格式,而不必将大量的string连接在一起! 例如:

 moment().format('hh:mm:ss'); //Will return 14:37:36 

我使用所有时间操作/显示需求(如果你使用它,客户端和node.js),如果你只是需要一个简单的格式上面的答案会做,如果你正在寻找一些更复杂的东西,时刻是走向国际海事组织的路。

 <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> </script> <script> function ShowLocalDate() { var dNow = new Date(); var localdate= (dNow.getMonth()+1) + '/' + dNow.getDate() + '/' + dNow.getFullYear() + ' ' + dNow.getHours() + ':' + dNow.getMinutes(); $('#currentDate').text(localdate) } </script> </head> <body> enter code here <h1>Get current local enter code here Date in JQuery</h1> <label id="currentDate">This is current local Date Time in JQuery</p> <button type="`enter code here button onclick="ShowLocalDate()">Show Local DateTime</button> </body> </html> 

你可以从下面的链接获得更多的信息

http://www.morgantechspace.com/2013/11/Get-current-Date-time-in-JQuery.html#GetLocalDateTimeinJQuery

尝试

 console.log( new Date().toLocaleString().slice(9, -3) , new Date().toString().slice(16, -15) ); 

使用JavaScript本地datefunction,您可以根据需要获得小时 , 分钟和秒钟 。 如果您希望以特定的方式格式化date和时间,则可能需要实现扩展JavaScript Date原型的方法。

这里已经实现了一个: https : //github.com/jacwright/date.format