DEV Community

Steven Frasica
Steven Frasica

Posted on

Current Date and Time in JavaScript

The current date and time in your web application can be a nice touch and convenient for the user to view. JavaScript has built in methods to help you display that information easily. Additionally, there are various ways to customize the date and time and display it to your liking. We'll begin with the basic methods and expand upon those.

Date object

You can get the date using the Date Object. Here's an example below which I will type in to the console.

let currentDate = new Date();

currentDate;

//Output is:  Sun Jul 12 2020 20:00:37 GMT-0400 (Eastern Daylight Time)

If you test this out in the console, it will look similar to this:

Alt Text

As a reminder, even though the date looks like a string, it is actually a date object that is returned to us.

From the date object, we can select specific information we want, such as the day.

We call the getDay() method on the date object which will return a number assigned to the day of the week. 0 for Sunday through 6 for Saturday.

currentDate.getDay();

//Output is: 0
0 stands in for Sunday.

There are various methods similar to getDay() to pull different info from the date object.

Methods:

  • getMonth() for the month (range is 0-11)
  • getDate() for which day of the month (range is 1-31)
  • getFullYear() for the year
  • getHours() for the hour (range is 0-23. 0 is midnight and 23 is 11pm)
  • getMinutes() for the minute (range is 0-59)
  • getMilliseconds() for the millisecond (range is 0-999)
  • getTime() for the number of milliseconds since Jan 1, 1970.

Try these methods out to get the date and time information you want to display.

Top comments (0)