55 Votes

JavaScript: Get current Date and Time

Tutorial by Stefan Trost | Last update on 2024-04-11 | Created on 2015-10-04

Today, I would like to show you how to retrieve the current date as well as the current time using JavaScript in order to use them in your scripts.

For both, date and time, we need the date object provided by JavaScript which we can create in the following way:

var today = new Date();

After that, we can read out all values we need to get the current date and the current time in order to process or output them in our script.

Determine current Date

First, let's look at how we can get the current date (we will come to the time in the next section). To do this, we first create our Date object and then we read out the year, the month and the day.

var today = new Date();
var d = today.getDate();
var m = today.getMonth() + 1;
var y = today.getFullYear();

var dmy = d + "/" + m + "/" + y;
alert(dmy);

To read out the individual components we use the methods .getDate(), .getMonth() as well as .getFullYear():

  • To get the day we use the .getDate() method. This provides us with the day of the month as a number from 1 to 31.
  • To get the month we then use the .getMonth() method. This time we have to add 1 to the value obtained, because JavaScript gives us the month as a number between 0 and 11. This is not necessary with .getDate(). The value 0 corresponds to January, the value 1 to February, up to the number 11, which stands for December.
  • Finally we use .getFullYear() to get the year for our date as well. It's called .getFullYear() because the method is different from the related .getYear() method. With .getFullYear() we get the year as a 4-digit number while .getYear() returns a 2-digit number for years between 1900 and 1999. From the year 2000 .getYear() resturns the year as 3 digits starting with 100, before 1900 .getYear() will return negative values. Because of this hitch (keyword: year 2000 problem) .getYear() has now been removed from the web standard and should no longer be used. For this reason, we should always use .getFullYear(), as .getYear() may at some point no longer be supported by browsers.

After we have received the current day, the current date as well as the current year as individual values, we combine the individual components into a date in the format day/month/year and output this via an alert() dialog.

Of course, this example is only intended to demonstrate the basic function, in practice we can of course combine the values as we wish, use them otherwise or instead of reading out all of them, we can also just read out only individual date components.

Determine current Time

We can also determine the current time via the Date object. The next example shows how to do that.

var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();

var t = h + ":" + m + ":" + s;
alert(t);

Analogous to the methods to get the components of the date, this time we use the methods .getHours(), .getMinutes() and .getSeconds() to get the current hour, minute and second. We get values from 0 to 59 respectively 0 to 23.

The Date object also provides us with the method .getMilliseconds() that can be used if we also need the milliseconds.

In our example we use the values again to display the current time via an alert() message.

ReplyPositiveNegative
22 Votes

If you do not need or miss the flexibility you have with getting each part of the date on its own, you can also use the functions .toLocaleString() or .toISOString() instead.

First, here are some examples of using .toLocaleString():

var date = new Date();

// 12/24/2023, 10:00:00 PM
var d1 = date.toLocaleString('en-US');

// 24/12/2023 10:00:00
var d2 = date.toLocaleString('en-GB');

// 24.12.2023 10:00:00
var d3 = date.toLocaleString('de-DE');

Depending on the parameter you are passing, you get the date in another formatting. For example, when using "en-US", you will get the date in the format month/day/year and with AM and PM. Using "en-GB", "es-ES" or "fr-FR", you get the format day/month/year or if you are using "de-DE" you get the German format day.month.year. You can also omit this parameter. With only calling .toLocaleString() without any parameter, the system or browser formatting is used.

var date = new Date();

// 2023-12-24T10:00:00.000Z
var d = date.toISOString();

Apart from that, the method .toISOString() is always delivering a string in the format YYYY-MM-DDTHH:mm:ss.sssZ, which always has a length of 24 characters and follows the ISO format 8601.
Last update on 2024-04-11 | Created on 2015-10-04

ReplyPositive Negative
Reply

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

PHP: Current Date and Time

Tutorial | 0 Comments

VirtualBox: Change Date and Time

Tutorial | 10 Comments

PHP: Determine Week Number

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.