Get current date without time in JavaScript
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:
|
1 2 3 4 5 6 7 8 9 |
// Create a new date object with the current date and time var date = new Date(); // Set the hours, minutes, seconds, and milliseconds to zero date.setHours(0, 0, 0, 0); // The date object now only contains the current date without time // e.g. 2020-09-20T00:00:00.000Z console.log(date); |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Create a new date object with the current date and time var date = new Date(); // Convert the date object to an ISO string var isoString = date.toISOString(); // Extract only the date part from the ISO string var dateString = isoString.slice(0, 10); // The dateString now contains the current date without time in YYYY-MM-DD format // e.g. "2020-09-20" console.log(dateString); |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Import Moment.js in our JavaScript file const moment = require("moment"); // Create a new moment object with the current date and time var momentObj = moment(); // Set the moment object to the start of the day momentObj.startOf("day"); // The moment object now only contains the current date without time // e.g. Moment<2020-09-20T00:00:00+05:30> console.log(momentObj); // We can also format the moment object to a string using its format function var dateString = momentObj.format("YYYY-MM-DD"); // The dateString now contains the current date without time in YYYY-MM-DD format // e.g. "2020-09-20" console.log(dateString); |
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:
|
1 2 3 4 5 6 7 8 |
// create a Date object with the current date and time const date = new Date(); // get the date string using the default locale const dateString = date.toLocaleDateString(); // prints date like "20/9/2020" console.log(dateString); |
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:
|
1 2 3 4 5 |
var nowDate = new Date(); var dateString = nowDate.getFullYear() + '/' + (nowDate.getMonth() + 1) + '/' + nowDate.getDate(); // This will return a string like "2020/9/20" console.log(dateString); |
That’s all about getting current date without time in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)