JavaScript program to read the current date time

JavaScript program to read the current date and time value without third party library:

In this post, we will learn how to read the current date and time value in a human-readable format. We can print the current date-time in different ways. In this post, we will learn different ways to do that.

toDateString() method:

toDateString method returns the value of a date object in human readable format. We can use Date() to get the current date-time and use toDateString method to print the value.

Below example shows how to use toDateString():

var date = new Date();
console.log(date.toDateString())

If you run this code, it will print the below output:

Sun Dec 20 2020

It is an easy way to get the date-time in a human-readable format.

Method 2: Get the date, time, day, etc. separately:

We can also use the below methods to get each date, time, day values separately:

getDay(): get the day of the week from 0 to 6 getDate(): get the day of the month from 1 to 31 getFullYear(): get the full year in four digits getHours(): get the hours from 0 to 23 getMinutes(): get the minutes from 0 to 59 getMonths(): get the months from 0 to 11

These methods can be used to form the date in the required format.

For example:

var date = new Date();

console.log(date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear())

It will print:

20/12/2020

You might also like: