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 you can read the current date and time of user system.
Let’s check how to get current date time in javascript.
How to Get Current Date and Time in Javascript
Let’s first create a date object.
/* 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)
How to Get Current Timestamp
We already created date object. Now to get current timestamp (in milliseconds), Javascript providesgetTime() method.
getTime() return the timestamp value in milliseconds as compare to other programming language such as PHP which returns the timestamp value in seconds.
/* get current timestamp. */
var currentTimestamp = date.getTime();
/* Alert the value of current timestamp. */
alert(currentTimestamp);
/* To display in console. */
console.log(currentTimestamp);
Print Current Year
To print current year in Javascript, javascript provides getFullYear() method.
var currentYear = date.getFullYear();
alert(currentYear);
console.log(currentYear);
getMonth() Method in Javascript – Display Current Month in Numeric
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.
var currentMonth = date.getMonth() + 1;
console.log(currentMonth);
How to Display Current Date in Javascript
We already discussed how to create date object and it’s useful methods.
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 object
var currentTime = new Date();
getHours() = It displays the current hour.
getMinutes() = Return current minute.
getSeconds() = Return the value of second.
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 discuss only few methods, 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.
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.