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:
javascriptlet 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:
javascriptlet 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.
sqllet 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.
javascriptconsole.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:
- 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)".
vbnetlet date = new Date();
console.log(date.toString());
- 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".
vbnetlet date = new Date();
console.log(date.toUTCString());
- toDateString(): The toDateString() method returns a string representation of the date in the format "Mon Jan 25 2021".
vbnetlet date = new Date();
console.log(date.toDateString());
- toISOString(): The toISOString() method returns a string representation of the date in ISO 8601 format, such as "2021-01-25T20:00:00.000Z".
vbnetlet date = new Date();
console.log(date.toISOString());
- 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".
vbnetlet date = new Date();
console.log(date.toLocaleString());
- 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".
vbnetlet date = new Date();
console.log(date.toLocaleDateString());
- 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".
vbnetlet 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:
- Comparing dates: You can use the comparison operators (
<
,>
,<=
,>=
) to compare two Date objects and determine which one comes first.
javascriptlet date1 = new Date("January 1, 2000 12:00:00");
let date2 = new Date("January 2, 2000 12:00:00");
console.log(date1 < date2); // true
- Setting the time: You can use the
setFullYear()
,setMonth()
,setDate()
,setHours()
,setMinutes()
,setSeconds()
andsetMilliseconds()
methods to set the various parts of the date and time.
vbnetlet date = new Date();
date.setFullYear(2022);
date.setMonth(0); // January
date.setDate(1);
console.log(date);
- 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.
sqllet date = new Date();
date.setTime(date.getTime() + (60 * 60 * 24 * 1000)); // add one day
console.log(date);
- 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.
vbnetlet date = new Date();
console.log(date.toLocaleDateString());
console.log(date.toLocaleTimeString());
- Getting time difference: You can use the
getTime()
method to get the number of milliseconds between two dates.
javascriptlet 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.
0 Comments