HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

JavaScript Date Formats

JavaScript provides several options for formatting dates.

Here are some commonly used methods:

  • toLocaleDateString():: This method returns a string with a language-sensitive representation of the date.
  • toISOString():: This method returns a string in simplified extended ISO format (ISO 8601).
  • toUTCString(): : This method converts a date to a string, using the UTC time zone.
  • toLocaleString():: This method returns a string representing the date and time in the current locale format.
  • Custom formatting::: You can also create custom date formats using methods like getFullYear(), getMonth(), getDate(), getHours(), getMinutes(), and getSeconds() and combining them with string concatenation or template literals.

toLocaleDateString()

Example

const date = new Date();
console.log(date.toLocaleDateString('en-US')); //
You can click on above box to edit the code and run again.

Output

4/26/2024

toISOString()

Example

const date = new Date();
console.log(date.toISOString()); //  

You can click on above box to edit the code and run again.

Output

2024-04-26T12:00:00.000Z

toUTCString():

Example

const date = new Date();
console.log(date.toUTCString()); 

You can click on above box to edit the code and run again.

Output

Fri, 26 Apr 2024 12:00:00 GMT

toLocaleString():

Example

const date = new Date();
console.log(date.toLocaleString()); 

You can click on above box to edit the code and run again.

Output

4/26/2024, 12:00:00 PM

Custom formatting:

Example

const date = new Date();
const formattedDate = `${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`;
console.log(formattedDate); 

You can click on above box to edit the code and run again.

Output

4/26/2024