Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove time part from JavaScript date?

Tags:

javascript

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?

like image 964
Phill Greggan Avatar asked Jan 11 '16 13:01

Phill Greggan


People also ask

How do I remove the timestamp from a date moment?

We can remove the time portion from a date with the startOf method. We pass in a date with the hour and minutes set into the moment function. Then we call startOf with the 'day' argument to get return a moment object with the time set to midnight of the same date.

How do I remove T and Z from timestamp?

To remove the T and Z characters from an ISO date in JavaScript, we first need to have the date in ISO format. If you don't have it already, you can convert it to ISO using the toISOString function on a Date object. This will get rid of both T and Z, resulting in "2022-06-22 07:54:52.657".

How do you subtract time in JavaScript?

To subtract hours from a date:Use the getHours() method to get the hours of the specific date. Use the setHours() method to set the hours for the date. The setHours method takes the hours as a parameter and sets the value for the date.


5 Answers

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

like image 90
Vijay Jagdale Avatar answered Oct 18 '22 08:10

Vijay Jagdale


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());
like image 30
Cerbrus Avatar answered Oct 18 '22 08:10

Cerbrus


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);
like image 25
Ibrahim Khan Avatar answered Oct 18 '22 07:10

Ibrahim Khan


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);
like image 44
kreshnov Avatar answered Oct 18 '22 09:10

kreshnov


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()

like image 27
sggranil Avatar answered Oct 18 '22 08:10

sggranil