How to Use the JavaScript Date Object?

Estimated read time 1 min read

The JavaScript Date object is used to work with dates and times in JavaScript. You can use the Date object to perform operations such as:

  • Get the current date and time
  • Format dates and times
  • Calculate the difference between two dates
  • Convert dates to timestamps

Here are a few examples of using the Date object in JavaScript:

  1. Get the current date and time:
let today = new Date();
  1. Format dates and times:
let date = new Date();
let options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(date.toLocaleDateString('en-US', options));
  1. Calculate the difference between two dates:
let start = new Date("2023-02-01");
let end = new Date("2023-02-09");
let diff = end - start;
console.log(diff / (1000 * 60 * 60 * 24) + " days");
  1. Convert dates to timestamps:
let date = new Date();

These are just a few examples of what you can do with the JavaScript Date object. You can find more information and examples in the JavaScript documentation.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply