JavaScript - Current Date/Time



Date and time are the most common things we often need to use in applications. In javascript, we can use Date object to get the current date and time.

To solve all the problems, we will just create an object of date class and will use its various methods to get the current date and time.

  • Date object
  • toLocaleString() method

Current Date and Time using Date Object

The Date class contains several methods in which we can fetch the current date, day, and time by. Also, it includes hundreds of different methods that programmers can use according to their requirements.

Syntax

Below is the syntax given to get the current date and time using the Date object.

var date = new Date();

Using the below syntax users can get the year, month and date.

var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();

Using the below syntax users can get hours, minutes, and second.

var date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();

Example

Following example explains how to use the above methods to get the date and time from the Date object. Also, we have added +1 to the returned value from the month as it returns values between 0 and 11.

<html>
<body>
< p id = "demo" ></p>
<script>
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
document.getElementById('demo').innerHTML = 'Current Date and Time: ' + day + '-' + month + '-' + year + ' ' + hours + ':' + minutes + ':' + seconds;
</script>
</body>
</html>

Output

Following is the output of the above program.

Current Date and Time: 15-12-2024 12:30:45

In the above output, users can see that we are getting the current date and time according to the local time zone.

Using toLocaleString() method

Here, we will use the Date object the same way as the above approach, But now we will use the toLocalString() method. It takes two parameters, one is locale, and another is options.

Locale means the local zone or region for which you want to take data and options is the object which contains many properties.

Syntax

Below is the syntax given to get the current date and time using the toLocaleString() method.

var date = new Date();
var current_date_and_time = date.toLocaleString();
var current_time = date.toLocaleTimeString();
var current_date = date.toLocaleDateString();

Users can follow the below syntax, if they want to get date and time for any local region.

var date = new Date();
var current_date_and_time = date.toLocaleString('en-US', { timeZone: 'America/New_York' });
var current_time = date.toLocaleTimeString('en-US', { timeZone: 'America/New_York' });
var current_date = date.toLocaleDateString('en-US', { timeZone: 'America/New_York' });

Parameters

  • locale : The local parameter represents the Unicode for the local region. If you want to get the current date and time of India, you should pass en-IN, for the US users can pass the en-US.
  • options : The options parameters includes a lots of properties to format the date and time. For example, in above syntax we have written that return the year and day in the numeric format and return the weekday and month in the long format.

Example

The below example demonstrate to use the toLocaleString() method to get the get and time for any specific region.

<html>
<body>
<script>
var date = new Date();
var current_date_and_time = date.toLocaleString();
var current_time = date.toLocaleTimeString();
var current_date = date.toLocaleDateString();
document.write('Current Date and Time: ' + current_date_and_time + '<br>');
document.write('Current Time: ' + current_time + '<br>');
document.write('Current Date: ' + current_date + '<br>');
</script>
</body>
</html>

Output

Following is the output of the above program.

Current Date and Time: 12/15/2024, 12:30:45 PM
Current Time: 12:30:45 PM
Current Date: 12/15/2024

In the above output, users can see that we are getting the current date and time according to the local time zone.

The Date class contains many advanced methods to get the date and time. However, users can get the whole date and time string using the built-in methods.

Users can get date and time information according to their requirements using various methods of date class. Also, toLocaleString() methods allow you to get regional dates and times. Furthermore, it will enable you to format the date and time according to your need by passing the options object as a parameter.

Advertisements