Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

JavaScript Date Methods

Updated on Nov 20, 2024
By Mari Selvan
👁️ 81 - Views
⏳ 4 mins
💬 1 Comment
JavaScript Date Methods

Photo Credit to CodeToFun

🙋 Introduction

JavaScript provides a variety of built-in Date methods that allow you to work with dates and times effectively. These methods enable you to retrieve, manipulate, and format date-related information, making it easier to handle temporal data in your JavaScript applications.

📑 Table of Contents

Some of the commonly used date methods in JavaScript includes:

MethodsExplanation
getDate()Retrieves the day of the month (from 1 to 31) for a specified date.
getFullYear()Returns the four-digit year, providing a straightforward way to extract the year component from a date.
getHours()Retrieves the hour of the day (0 to 23) from a Date object.
getMilliseconds()Returns the milliseconds portion of the time representation.
getMinutes()Returns the minutes of the specified date and time, ranging from 0 to 59.
getMonth()Returns the month (0 to 11) for a specified date.
getSeconds()Retrieves the seconds component of a date object.
getTime()Returns the numeric value representing the milliseconds elapsed since January 1, 1970, providing a timestamp for date and time calculations.
getTimezoneOffset()Returns the time zone difference between the local machine's time zone and Coordinated Universal Time (UTC) in minutes.
getUTCDate()Retrieves the day of the month (1-31) according to Coordinated Universal Time (UTC).
getUTCDay()Returns the day of the week (0 for Sunday, 1 for Monday, and so on) of a specified date in Coordinated Universal Time (UTC).
getUTCFullYear()Returns the year of a date in Coordinated Universal Time (UTC).
getUTCHours()Returns the hours in Coordinated Universal Time (UTC) for a specified date.
getUTCMilliseconds()Returns the milliseconds of a date object in Coordinated Universal Time (UTC).
getUTCMinutes()Returns the minutes in Coordinated Universal Time (UTC) from a Date object..
getUTCMonth()Returns the month (from 0 to 11) of a date, according to Coordinated Universal Time (UTC).
getUTCSeconds()Returns the seconds (0 to 59) of a specified date and time in Coordinated Universal Time (UTC).
now()Returns the current timestamp in milliseconds, providing a convenient way to capture the current time for various time-related operations in web development.
parse()Parses a date string and returns the number of milliseconds since January 1, 1970, known as the Unix timestamp.
setDate()Sets the day of the month for a specified date.
setFullYear()Sets the year for a specified date.
setHours()Used to set the hour value of a Date object, allowing for easy manipulation of time components.
setMilliseconds()Used to set the milliseconds for a specified date, altering the Date object in place.
setMinutes()Used to set the minutes for a specified date object.
setMonth()Used to set the month for a specified date, allowing easy manipulation of date objects.
setSeconds()Sets the seconds for a specified date object, allowing precise manipulation of time components.
setTime()Sets the time value for a Date object, allowing precise control over hours, minutes, seconds, and milliseconds.
setUTCDate()Used to set the day of the month of a date object in Coordinated Universal Time (UTC).
setUTCFullYear()Used to set the year of a date object in Coordinated Universal Time (UTC).
setUTCHours()Sets the hour value of a date object, according to Coordinated Universal Time (UTC), providing a convenient way to manipulate time components.
setUTCMilliseconds()Allows for setting the milliseconds portion of a date object, based on Coordinated Universal Time (UTC).
setUTCMinutes()Sets the minutes for a specified date object in Coordinated Universal Time (UTC).
setUTCMonth()Sets the month (from 0 to 11) in a date object, according to Universal Coordinated Time (UTC).
setUTCSeconds()Sets the seconds for a specified date according to universal time, allowing precise manipulation of time components in a Date object.
toDateString()Converts the date portion of a Date object into a human-readable string representation.
toISOString()Converts a Date object to a string in ISO format, representing the date and time in Coordinated Universal Time (UTC).
toJSON()Converts the date to a string in JSON format.
toLocaleDateString()Converts a date to a string, representing the date portion according to the locale-specific formatting rules.
toLocaleTimeString()Converts a date object to a string representing the time portion in a locale-specific format.
toLocaleString()Converts a date to a string with a locale-specific format, considering factors like the date, time, and numeric representations based on the user's locale.
toString()Converts the date to a human-readable string, providing a standard representation of the date and time.
toTimeString()Returns a string representing the time portion of a Date object in human-readable format.
toUTCString()Converts a date to a string in UTC format, providing a human-readable representation of the date and time in Coordinated Universal Time.
UTC()Returns the number of milliseconds in a date since January 1, 1970, 00:00:00 UTC, allowing the creation of a date object based on a specified UTC time.
valueOf()Returns the numeric value corresponding to the time of a Date object.

🚀 Usage Tips

Explore the following tips to make the most out of JavaScript date methods:

  1. Timezone Considerations:

    Be aware of the timezone differences when working with date and time methods.

  2. Chaining Methods:

    Chain multiple Date methods to perform complex date manipulations in a single line.

  3. Parsing Dates:

    Use the Date.parse() method to parse date strings and convert them into JavaScript Date objects.

  4. Formatting Dates:

    Leverage the various formatting methods to present date information in a human-readable format.

📝 Example

Let's look at a practical example of using JavaScript Date methods to retrieve and format date-related information:

example.js
Copied
Copy To Clipboard
// Example: Using JavaScript Date methods

const currentDate = new Date();

console.log(`Current Date: ${currentDate.toDateString()}`);
console.log(`Current Time: ${currentDate.toLocaleTimeString()}`);
console.log(`Day of the Week: ${currentDate.toLocaleDateString('en-US', { weekday: 'long' })}`);

This example showcases the use of toDateString(), toLocaleTimeString(), and toLocaleDateString() to format the current date, time, and day of the week. Experiment with different methods and parameters to explore the capabilities of JavaScript Date methods.

🎉 Conclusion

This reference guide provides an overview of essential JavaScript Date methods. By incorporating these methods into your code, you can efficiently work with date and time values in your JavaScript applications. Experiment with these methods to enhance your understanding and handling of temporal data.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy