In javascript, many built-in functions work with a Date object. As for set only the date ignoring time in JavaScript, we can also do it with some functions in JavaScript. Here are some examples to understand it well.
Set only the date ignoring time in JavaScript
To set only the date ignoring time in JavaScript, we have some ways:
- Initialize Date with a string Long Date(MMM DD YYYY) or Short Date(MM/DD/YYYY)
- Reset the time of day.
Initialize the Date with a string Long Date or Short Date
To do this, we must get the string Long Date(MMM DD YYYY) or Short Date(MM/DD/YYYY). I used the toLocaleString() Method to get them out quite simply.
Syntax:
toLocaleString(locales, options)
Parameters:
- locales: is a string, usually your browser’s language.
- options: an object that stores the format of the date you want to output. Suppose you want to learn more; visit the W3C docs.
Example:
var date = new Date();
// Create options
var options = {
dateStyle: "short"
};
console.log(date);
console.log(date.toLocaleString("en-US", options));
Output:
Fri Oct 28 2022 22:09:28
10/28/22
In the above code, I have retrieved the machine’s Short Date of the current date.
I will combine the above code to get a date with a time of 00:00:00.
Example:
var date = new Date();
// Create options
var options = {
dateStyle: "long"
};
console.log(date);
console.log(date.toLocaleString("en-US", options));
Output:
Fri Oct 28 2022 22:09:28
October 31, 2022
In the above example, I used the current system date to test. We see when I use a string Short Date format to initialize a new date, the date will be created with a time of 00:00:00.
Similar to the Long Date format, we will also get the same result to do it. We need to change the value of the dateStyle property in the options object.
Also, we can use the return string of the toDateString() Method. I will do it like this:
var date = new Date();
var dateString = date.toDateString(); // Fri Oct 28 2022
var resetDate = new Date(dateString);
// Compare two results
console.log(date);
console.log(resetDate);
Output:
Fri Oct 28 2022 22:09:28
Fri Oct 28 2022 00:00:00
Reset the time of day
Reset the date time using the setHours() Method.
Syntax:
date.setHours(hours, minutes, seconds)
Parameters:
- hours: is the value you want to change for the hour of the date you are considering.
- minutes: is the value you want to change for the minutes of the date you are considering.
- seconds: is the value you want to change for the seconds of the date you are considering.
Example:
var date = new Date();
console.log(date);
// Set the time to 00:00:00
date.setHours(0,0,0);
console.log(date);
Output:
Fri Oct 28 2022 22:09:28
Fri Oct 28 2022 00:00:00
In this example, we will reset the time of our newly created date to 00:00:00. This will change the variable instead of creating the variable with and saving the value.
Summary
In this Article, I showed you several ways to set only the date ignoring time in JavaScript. You should practice and read together to understand and remember how to solve it. Hope this Article will help you and your program. Good luck.
Maybe you are interested:
- Increment a Date by 7 Days in JavaScript
- Convert HH:MM:SS to Seconds using JavaScript
- Convert Minutes to Hours and Minutes in JavaScript

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css