All about Date in Javascript

 All about Date() in Javascript


In JavaScript, the Date object is used to work with dates and times. You can create a new Date object by calling the Date() constructor, which will give you the current date and time. For example:

javascript
let today = new Date(); console.log(today);

You can also create a Date object for a specific date and time by passing in the date and time as arguments to the constructor. For example:

javascript
let birthday = new Date("January 1, 2000 12:00:00"); console.log(birthday);

You can also use the various methods of the Date object to extract information such as the year, month, and day, or to perform operations such as setting the time or comparing dates.

sql
let today = new Date(); let year = today.getFullYear(); let month = today.getMonth(); let day = today.getDate(); console.log(`Year: ${year}, Month: ${month}, Day: ${day}`);

Also, you can use the .toString() method to get the string format of the date.

javascript
console.log(today.toString());

You can also use other libraries such as moment.js for more advanced date manipulation.

Different Date formats

There are several ways to format a JavaScript date, depending on the desired output. Here are a few examples:

  1. toString(): The toString() method returns a human-readable string representation of the date, such as "Mon Jan 25 2021 12:00:00 GMT-0800 (Pacific Standard Time)".
vbnet
let date = new Date(); console.log(date.toString());
  1. toUTCString(): The toUTCString() method returns a string representation of the date in UTC (Coordinated Universal Time) format, such as "Mon, 25 Jan 2021 20:00:00 GMT".
vbnet
let date = new Date(); console.log(date.toUTCString());
  1. toDateString(): The toDateString() method returns a string representation of the date in the format "Mon Jan 25 2021".
vbnet
let date = new Date(); console.log(date.toDateString());
  1. toISOString(): The toISOString() method returns a string representation of the date in ISO 8601 format, such as "2021-01-25T20:00:00.000Z".
vbnet
let date = new Date(); console.log(date.toISOString());
  1. toLocaleString(): The toLocaleString() method returns a string representation of the date in a format that is appropriate for the current locale, such as "Monday, January 25, 2021, 12:00:00 PM".
vbnet
let date = new Date(); console.log(date.toLocaleString());
  1. toLocaleDateString(): The toLocaleDateString() method returns a string representation of the date in a format that is appropriate for the current locale, such as "Monday, January 25, 2021".
vbnet
let date = new Date(); console.log(date.toLocaleDateString());
  1. toLocaleTimeString(): The toLocaleTimeString() method returns a string representation of the time in a format that is appropriate for the current locale, such as "12:00:00 PM".
vbnet
let date = new Date(); console.log(date.toLocaleTimeString());

You can also use third-party libraries such as moment.js to format your dates in more advanced ways.

Operations Using Date Function

There are many operations that you can perform on JavaScript dates. Here are a few examples:

  1. Comparing dates: You can use the comparison operators (<, >, <=, >=) to compare two Date objects and determine which one comes first.
javascript
let date1 = new Date("January 1, 2000 12:00:00"); let date2 = new Date("January 2, 2000 12:00:00"); console.log(date1 < date2); // true
  1. Setting the time: You can use the setFullYear(), setMonth(), setDate(), setHours(), setMinutes(), setSeconds() and setMilliseconds() methods to set the various parts of the date and time.
vbnet
let date = new Date(); date.setFullYear(2022); date.setMonth(0); // January date.setDate(1); console.log(date);
  1. Adding and subtracting time: You can use the setTime() method to add or subtract a certain number of milliseconds from a Date object, which can be useful for performing calculations.
sql
let date = new Date(); date.setTime(date.getTime() + (60 * 60 * 24 * 1000)); // add one day console.log(date);
  1. Formatting the date: You can use the toString(), toUTCString(), toDateString(), toISOString(), toLocaleString(), toLocaleDateString(), toLocaleTimeString() methods to get the string representation of the date in a specific format.
vbnet
let date = new Date(); console.log(date.toLocaleDateString()); console.log(date.toLocaleTimeString());
  1. Getting time difference: You can use the getTime() method to get the number of milliseconds between two dates.
javascript
let date1 = new Date(); let date2 = new Date("January 2, 2022 12:00:00"); let diff = date2.getTime() - date1.getTime(); console.log(diff);

These are just a few examples of the many operations you can perform on JavaScript dates. There are many more methods and properties available on the Date object that can be used for more advanced date manipulation.

Post a Comment

0 Comments