How To Remove The Time From A Date Using JavaScript

Remove the Time from a Date using JavaScript

To remove the time from a date using JavaScript. We have effectively tested with three methods: Using the toLocaleDateString() method, the toDateString() method, and the split() method. Check out the details below to get more information.

Remove the time from a date using JavaScript

Using the toLocaleDateString() method

To remove the time from a date using JavaScript,  you could use the toLocaleDateString() method in Javascript. To get more information about this method, please read another us article here.   

Example

In this example, we will create a date variable to get the current date time. Then we will use toLocaleDateString() to get only the date from the date object and remove the time as well.

var date = new Date();
console.log("The current date time is: ", date);

// Use toLocaleDateString() to get only the date and remove time in the 'date' object
var noTime = date.toLocaleDateString("en-CA");
console.log("After removing time and getting only the date: ", noTime);

Output

The current date time is: Thu Oct 06 2022 21:44:57 GMT+0700 
After removing time and getting only the date: 2022-10-06

Using the split() method

The split() method is used to split a string into an string array and return a new array.

Syntax:

split(separator, limit)

Parameters: 

  • separator: A string or regular expression to use for splitting.
  • limit: An integer that limits the number of the split.

Return value: A new array of strings.

Example:

In this example, we will create a dateTime variable to get the current date time. Then we will use the split() method to split the string by using a regular expression to two elements and get the first element as a date object.

var dateTime = new Date();

// Convert 'dateTime' to String
dateTime = String(dateTime);
console.log("Date and time: ", dateTime);

// Split the String by using the regular expression
var noTime = dateTime.split(/(.*)\s\d{2}:/)[1];
console.log("After removing the time value: ", noTime);

Output

Date and time: Thu Oct 06 2022 22:01:44 GMT+0700 
After removing the time value: Thu Oct 06 2022

Using the toDateString() method

Another simple way to remove the time from a date is to use the toDateString() method.

Syntax:  

toDateString()

Return value: A string representing the date.

Example:

In this example, we will create a date variable to get the current date time. Then we will use toDateString() to get only the date from the date object and also remove the time .

var date = new Date();
console.log("The current date time is: ", date);

// Use toDateString() to get only the date and remove time in the 'date' object
var noTime = date.toDateString();
console.log("After removing time and getting only the date: ", noTime);

Output

The current date time is: Thu Oct 06 2022 22:04:55 GMT+0700 
After removing time and getting only the date: Thu Oct 06 2022

Summary

In this tutorial, we have explained how to remove the time from a date using JavaScript by using three methods. We always hope this tutorial is helpful to you. Leave your comment here if you have questions or comments about improving the article. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *