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:
- Get the current date and time:
let today = new Date();
console.log(today);
- 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));
- 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");
- Convert dates to timestamps:
let date = new Date();
console.log(date.getTime());
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.
+ There are no comments
Add yours