How to remove time part from Date?

Javascript

Javascript Problem Overview


I have a date '12/12/1955 12:00:00 AM' stored in a hidden column. I want to display the date without the time.

How do I do this?

Javascript Solutions


Solution 1 - Javascript

This is probably the easiest way:

new Date(<your-date-object>.toDateString());

Example: To get the Current Date without time component:

new Date(new Date().toDateString());

gives: Thu Jul 11 2019 00:00:00 GMT-0400 (Eastern Daylight Time)

Note this works universally, because toDateString() produces date string with your browser's localization (without the time component), and the new Date() uses the same localization to parse that date string.

You can extend the Date object as below, so and then use the dateOnly property:

Date.prototype.getDateWithoutTime = function () {
    return new Date(this.toDateString());
}

Now <any-date-object>.getDateWithoutTime(); will output Date only

Solution 2 - Javascript

Parse that string into a Date object:

var myDate = new Date('10/11/1955 10:40:50 AM');

Then use the usual methods to get the date's day of month (getDate) / month (getMonth) / year (getFullYear).

var noTime = new Date(myDate.getFullYear(), myDate.getMonth(), myDate.getDate());

Solution 3 - Javascript

Split it by space and take first part like below. Hope this will help you.

var d = '12/12/1955 12:00:00 AM';
d = d.split(' ')[0];
console.log(d);

Solution 4 - Javascript

The previous answers are fine, just adding my preferred way of handling this:

var timePortion = myDate.getTime() % (3600 * 1000 * 24);
var dateOnly = new Date(myDate - timePortion);

If you start with a string, you first need to parse it like so:

var myDate = new Date(dateString);

And if you come across timezone related problems as I have, this should fix it:

var timePortion = (myDate.getTime() - myDate.getTimezoneOffset() * 60 * 1000) % (3600 * 1000 * 24);

Solution 5 - Javascript

previous answers are good. This is my method

var todaysDate = new Date; todaysDate = todaysDate.toDateString(); console.log(todaysDate);

Solution 6 - Javascript

This is perhaps the most effective solution.

var date = new Date().toLocaleDateString();

Example code below:

var dateToday = '2/19/2022, 12:00:00 AM';
var date = new Date(dateToday).toLocaleDateString();
console.log(date); // Output: 2/19/2022

Documentation: MDN Web Docs - Date.prototype.toLocaleDateString()

Solution 7 - Javascript

Sorry, I like oneliners. Given that original date d can be replaced:

d.setHours(-d.getTimezoneOffset() / 60, 0, 0, 0))

Or, maybe the sorter and less destructive:

new Date(d.toJSON().substr(0, 10))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhill GregganView Question on Stackoverflow
Solution 1 - JavascriptVijay JagdaleView Answer on Stackoverflow
Solution 2 - JavascriptCerbrusView Answer on Stackoverflow
Solution 3 - JavascriptIbrahim KhanView Answer on Stackoverflow
Solution 4 - JavascriptkreshnovView Answer on Stackoverflow
Solution 5 - JavascriptHaydenView Answer on Stackoverflow
Solution 6 - JavascriptsggranilView Answer on Stackoverflow
Solution 7 - JavascriptRobertoView Answer on Stackoverflow