This post will discuss how to get the current date without time in JavaScript.

There are several ways to get the current date without time in JavaScript. A date is a representation of a specific point in time, which can include the year, month, day, hour, minute, second, and millisecond. However, sometimes we may only need the date part without the time part. Here are some of the functions we can use:

1. Using Date.prototype.setHours() function

The Date.prototype.setHours() function sets the hours, minutes, seconds, and milliseconds of a date object to the specified values. We can use this function to set all these values to zero, effectively removing the time part from the date. For instance:

Download  Run Code

2. Using Date.prototype.toISOString() function

The Date.prototype.toISOString() function converts a date object to a string in a simplified extended ISO format, which follows the format YYYY-MM-DDTHH:mm:ss.sssZ. We can use this function to get the string representation of the date object, and then use the slice() function to extract only the date part from it. For example, the following code gets the current date as a string in ISO 8601 format and then slice the first 10 characters of the string.

Download  Run Code

3. Using Moment.js library

This is a more convenient way to get the current date without time in JavaScript. Moment.js is a popular library that provides various functions for manipulating and formatting dates. To get the current date without time using Moment.js, we can use its startOf() function. This function sets a date object to the start of a unit of time, such as day, month, year, etc. For instance:

Download Code

4. Using Date.prototype.toLocaleDateString() function

The Date.prototype.toLocaleDateString() built-in function converts a Date object to a string using a specific locale or language. We can use this function to get the date without the time in a format that is suitable for our region or preference. For instance:

Download  Run Code

5. Using Date.prototype.get*() functions

Finally, we can use the Date.prototype.getFullYear(), Date.prototype.getMonth(), and Date.prototype.getDate() functions to get the year, month, and day components of the current date. We can use these functions to get the individual components of the date and format them as we like. For instance:

Download  Run Code

That’s all about getting current date without time in JavaScript.