How to get current date and time in Javascript. How to display current date and time in Javascript.
In javascript date object is used to get current date and time. Using javascript date object our script can display and use the current date and time of a user system.
Let’s check how to get current date time in javascript.
Current Date and Time in Javascript
Let’s first create a date object.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/* Create date object. */ var date = new Date(); /* Alert date object value. */ alert(date); /* Check the value in console. */ console.log(date); // It will Print - Sat Oct 24 2015 08:17:39 GMT+0530 (India Standard Time) |
Difference between document.ready and window.onload .
How to validate the date in YYYY-MM-DD format using Javascript.
How to Get Current Timestamp in Javascript
We already created a date object. Now to get current timestamp (in milliseconds), Javascript provides getTime() method.
Javascript getTime() method return the timestamp value in milliseconds, As compare to other programming languages such as PHP which returns the timestamp value in seconds.
1 2 3 4 5 6 7 8 9 10 11 |
/* get current timestamp. */ var currentTimestamp = date.getTime(); /* Alert the value of current timestamp. */ alert(currentTimestamp); /* To display in console. */ console.log(currentTimestamp); |
How to convert the timestamp to date and time format in MySql .
getFullYear() Method – Print Current Year in Javascript
To print current year in Javascript, javascript provides the getFullYear() method.
How to check value exists in an array using Javascript/Jquery.
Use date object and call getFullYear() method.
1 2 3 4 |
var currentYear = date.getFullYear(); alert(currentYear); console.log(currentYear); |
getMonth() Method in Javascript – Display Current Month in Numeric
The getMonth() function return numeric value of month in the range of 0 to 11.
It starts from 0, so to get the current month we need to add plus one.
1 2 3 |
var currentMonth = date.getMonth() + 1; console.log(currentMonth); |
How to Display Current Date in Javascript
We already discussed how to create a date object and it’s useful methods.
1 2 3 4 5 6 7 8 |
var date = new Date(); currentDate = date.getDate(); // Get current date month = date.getMonth() + 1; // current month year = date.getFullYear(); alert("current date is " + currentDate + "/" + month + "/" + year); console.log("current date is " + currentDate + "/" + month + "/" + year); |
How to Display Current Time in Javascript
To display current time in javascript let’s first create a date object.
var currentTime = new Date();
getHours() = It displays the current hour.
getMinutes() = Return current minute.
getSeconds() = Return the value of second.
1 2 3 4 5 6 7 8 |
var currentTime = new Date(); hour = currentTime.getHours(); min = currentTime.getMinutes(); sec = currentTime.getSeconds(); alert("current Time is" + hour + ":" + min + ":" + sec); console.log("current Time is " + hour + ":" + min + ":" + sec); |
List of Methods for Date Object
I have discussed only a few methods for date object, here is a complete list of methods which you can use with date object.
getDate: Returns the day of the month (1-31) for the specified date according to local time.
getDay: Returns the day of the week (0-6) for the specified date according to local time.
getFullYear: Returns the year (4 digits for 4-digit years) of the specified date according to local time.
getHours: Returns the hour (0-23) in the specified date according to local time.
getMilliseconds: Returns the milliseconds (0-999) in the specified date according to local time.
getMinutes: Returns the minutes (0-59) in the specified date according to local time.
getMonth: Returns the month (0-11) in the specified date according to local time.
getSeconds: Returns the seconds (0-59) in the specified date according to local time.
getTime: Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC (negative for prior times).
getTimezoneOffset: Returns the time-zone offset in minutes for the current locale.
getUTCDate: Returns the day (date) of the month (1-31) in the specified date according to universal time.
getUTCDay: Returns the day of the week (0-6) in the specified date according to universal time.
getUTCFullYear: Returns the year (4 digits for 4-digit years) in the specified date according to universal time.
getUTCHours: Returns the hours (0-23) in the specified date according to universal time.
getUTCMilliseconds: Returns the milliseconds (0-999) in the specified date according to universal time.
getUTCMinutes: Returns the minutes (0-59) in the specified date according to universal time.
getUTCMonth: Returns the month (0-11) in the specified date according to universal time.
getUTCSeconds: Returns the seconds (0-59) in the specified date according to universal time.
getYear: Returns the year (usually 2-3 digits) in the specified date according to local time. Use getFullYear instead.
setDate: Sets the day of the month (1-31) for a specified date according to local time.
setFullYear: Sets the full year (4 digits for 4-digit years) for a specified date according to local time.
setHours: Sets the hours (0-23) for a specified date according to local time.
setMilliseconds: Sets the milliseconds (0-999) for a specified date according to local time.
setMinutes: Sets the minutes (0-59) for a specified date according to local time.
setMonth: Sets the month (0-11) for a specified date according to local time.
setSeconds: Sets the seconds (0-59) for a specified date according to local time.
setTime: Sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC, allowing for negative numbers for times prior.
setUTCDate: Sets the day of the month (1-31) for a specified date according to universal time.
setUTCFullYear: Sets the full year (4 digits for 4-digit years) for a specified date according to universal time.
setUTCHours: Sets the hour (0-23) for a specified date according to universal time.
setUTCMilliseconds: Sets the milliseconds (0-999) for a specified date according to universal time.
setUTCMinutes: Sets the minutes (0-59) for a specified date according to universal time.
setUTCMonth: Sets the month (0-11) for a specified date according to universal time.
setUTCSeconds: Sets the seconds (0-59) for a specified date according to universal time.
setYear: Sets the year (usually 2-3 digits) for a specified date according to local time. Use setFullYear instead.
toDateString: Returns the “date” portion of the Date as a human-readable string.