Updated April 18, 2023
Introduction to jQuery now
The jQuery now() function is used to get the current time. It is a built-in function in jQuery. It returns the number which specifies the current time since 1 January 1970 to current time. It specifies the current time in milliseconds. In simple the jQuery now() function is a short expression of the “(new Date).getTime();” expression.
Syntax:
$.now();
Parameters:
- It does not accept any parameters.
Return Value:
The return value of this function is the current time in milliseconds.
Working
- The jQuery now() function does not accept any parameter.
- Suppose we have to print the current time on the page, so we can use the now() function as “$.now();”, which will return the current time in a millisecond as a number, from which we can calculate the number of years, months, days, hours and minutes.
Examples of jQuery now
Given below are the examples mentioned:
Example #1
Example of jQuery now() function to get the current time.
Code:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<title> This is an example for jQuery now() function </title>
</head>
<style>
p {
color : blue;
</style>
<body>
<h3> The example for array now() function : </h3>
<button onclick = "checkRes()" > Click here to get the current time. </button>
<br>
<p id = "p1"> </p>
<p id = "p2"> </p>
<br>
<script>
function checkRes()
{
var time = $.now();
$( "#p1" ).text("The current time in milliseconds is : " + time );
$( "#p2" ).text("The current time in seconds is : " + time/1000 );
}
</script>
</body>
</html>
Output:
Once we click on the button, the output is:
In the above code, once we click on the button it will call to the checkRes() function, where it is used the now() function to display the current time in milliseconds from January 1, 1970, as “$.now();”, so we can see the current time in the above output.
Example #2
Function to get the current time using the Date object.
Code:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<title> This is an example for jQuery now() function </title>
</head>
<body>
<h3> The example for array now() function : </h3>
<button onclick = "checkRes()" > Click here to get the current time. </button>
<br>
<p id = "p1"> </p>
<p id = "p2"> </p>
<br>
<script>
function checkRes( )
{
var time = Date.now();;
alert( "The current time in milliseconds is : " + time + ". \nThe current time in seconds is : " + time/1000 );
}
</script>
</body>
</html>
Output:
Once we click on the first button, the output is:
In the above code, once we click on the button it will call to the checkRes() function, where the checkRes() function is used the now() function with the Date object to display the current time in milliseconds as “Date.now();”, so we can see the current time in the above output.
Example #3
Function to get the number of years, days, hours, minutes and seconds.
Code:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<title> This is an example for jQuery now() function </title>
</head>
<style>
p {
color : green;
</style>
<body>
<h3> The example for array now() function : </h3>
<button onclick = "checkRes()" > Click here to get the current time. </button>
<br>
<p id = "p1"> </p>
<p id = "p2"> </p>
<p id = "p3"> </p>
<p id = "p4"> </p>
<p id = "p5"> </p>
<br>
<script>
function checkRes()
{
var min = 1000 * 60;
var hrs = min * 60;
var days = hrs * 24;
var yrs = days * 365;
var time = $.now();
var yr = Math.round(time / yrs);
var d = Math.round(time / days);
var h = Math.round(time / hrs);
var m = Math.round(time / min);
$( "#p1" ).text("The number of years since midnight, January 1, 1970 is : " + yr );
$( "#p2" ).text("The number of days since midnight, January 1, 1970 is : " + d );
$( "#p3" ).text("The number of hours since midnight, January 1, 1970 is : " + h );
$( "#p4" ).text("The number of minutes since midnight, January 1, 1970 is : " + m );
$( "#p5" ).text("The number of seconds since midnight, January 1, 1970 is : " + time/1000 );
}
</script>
</body>
</html>
Output:
Once we click on the button, the output is:
In the above code, once we click on the button it will call to the checkRes() function, where the checkRes() function is used the now() function to get the current time in milliseconds from January 1, 1970, as “$.now();” and calculate the number of years, days, hours, minutes and seconds from January 1, 1970, to now, so we can see in the above output.
Conclusion
It is a built-in function, which is used to return the current time in milliseconds since January 1, 1970.
Recommended Articles
This is a guide to jQuery now. Here we discuss the introduction, working of jQuery now() function along with examples respectively. You may also have a look at the following articles to learn more –