How to get current time with jQuery

后端 未结 15 880
青春惊慌失措
青春惊慌失措 2020-12-07 08:10

The following returns time in microseconds, for example 4565212462.

alert( $.now() );

How do I convert it to a human readable time format,

15条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 09:00

    You don't need to use jQuery for this!

    The native JavaScript implementation is Date.now().

    Date.now() and $.now() return the same value:

    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)
    

    ..and if you want the time formatted in hh-mm-ss:

    var now = new Date(Date.now());
    var formatted = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
    // 20:10:58
    

提交回复
热议问题