How to Get Current Date and Time in Javascript

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.

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.

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.

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.

How to Display Current Date in Javascript

We already discussed how to create a date object and it’s useful methods.

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.

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.

Tagged , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.