[jquery] How to get current time with jQuery

The following returns time in microseconds, for example 4565212462.

alert( $.now() );

How do I convert it to a human readable time format, such as (hours:minutes:seconds)?

This question is related to jquery datetime time unix-timestamp microtime

The answer is


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


Try

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


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


Using JavaScript native Date functions you can get hours, minutes and seconds as you want. If you wish to format date and time in particular way you may want to implement a method extending JavaScript Date prototype.

Here is one already implemented: https://github.com/jacwright/date.format


jQuery's $.now() is an alias of new Date().getTime(), an internal Javascript function.

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

This returns the number of seconds elapsed since 1970, commonly referred to (not necessarily correctly) as Unix Time, Epoch or Timestamp, depending on the circles you fall in. It can be really handy for calculating the difference between dates/times using simple maths. It doesn't have any TimeZone information and is always UTC.

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

There is no need to use jQuery as other than this alias, it does little else to help with date/time manipulation.

If you are looking for a quick and dirty way of representing the time in text, the Javascript Date object has a "toString" prototype that will return an ISO formatted Date Time.

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

More than likely though, you will want to customize your formatting. The Date object has the ability to pull out your relevant details so you can build your own string representation.

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

However, as you have asked for a jQuery solution - it is perhaps likely that you are working with older browsers. If you want to do more specific things - especially interpreting strings into Date objects (useful for API responses), you might want to look at Moment.js.

http://momentjs.com/

This will ensure cross browser compatibility and has much improved formatting without having to concatenate lots of strings to together! For example:

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

jQuery.now() Returns: Number

Description: Return a number representing the current time.

This method does not accept any arguments.

The $.now() method is a shorthand for the number returned by the expression (new Date).getTime().

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

It's simple to use Javascript:

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

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

you can get more information from below link

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


You need to fetch all "numbers" manually

like this:

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


The following

function gettzdate(){
    var fd = moment().format('YYYY-MM-DDTHH:MM:ss');
    return fd ; 
}

works for forcing the current date onto a <input type="datetime-local">


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

For local time in ISO8601 for SQL TIMESTAMP you could try:

var tzoffset = (new Date()).getTimezoneOffset() * 60000;
var localISOTime = (new Date(Date.now() - tzoffset))
  .toISOString()
  .slice(0, 19)
  .replace('T', ' ');
$('#mydatediv').val(localISOTime);

_x000D_
_x000D_
<p id="date"></p>_x000D_
_x000D_
<script>_x000D_
var d = new Date();_x000D_
document.getElementById("date").innerHTML = d.toTimeString();_x000D_
</script>
_x000D_
_x000D_
_x000D_

You can use Date() in JS.


Convert a Date object to an string, using one of Date.prototype's conversion getters, for example:

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"

Or, if you want it more customized, see the list of Date.prototype's getter methods.


I use moment for all my time manipulation/display needs (both client side, and node.js if you use it), if you just need a simple format the answers above will do, if you are looking for something a bit more complex, moment is the way to go IMO.


Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to datetime

Comparing two joda DateTime instances How to format DateTime in Flutter , How to get current time in flutter? How do I convert 2018-04-10T04:00:00.000Z string to DateTime? How to get current local date and time in Kotlin Converting unix time into date-time via excel Convert python datetime to timestamp in milliseconds SQL Server date format yyyymmdd Laravel Carbon subtract days from current date Check if date is a valid one Why is ZoneOffset.UTC != ZoneId.of("UTC")?

Examples related to time

Date to milliseconds and back to date in Swift How to manage Angular2 "expression has changed after it was checked" exception when a component property depends on current datetime how to sort pandas dataframe from one column Convert time.Time to string How to get current time in python and break up into year, month, day, hour, minute? Xcode swift am/pm time to 24 hour format How to add/subtract time (hours, minutes, etc.) from a Pandas DataFrame.Index whos objects are of type datetime.time? What does this format means T00:00:00.000Z? How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift? Extract time from moment js object

Examples related to unix-timestamp

Converting unix time into date-time via excel Convert python datetime to timestamp in milliseconds Getting current unixtimestamp using Moment.js How to parse unix timestamp to time.Time Go / golang time.Now().UnixNano() convert to milliseconds? Get current NSDate in timestamp format MYSQL query between two timestamps How to get current time with jQuery Convert unix time to readable date in pandas dataframe How to get the unix timestamp in C#

Examples related to microtime

How to get current time with jQuery