Open In App

How to remove time from date using JavaScript ?

Last Updated : 20 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a Date Object and the task is to remove the time portion from the object using JavaScript

JavaScript split() Method: This method is used to split a string into an array of substrings, and returns the new array. 

Syntax:

string.split( separator, limit )

Parameters:

  • separator: This parameter is optional. It specifies the character or the regular expression to use for splitting the string. If not used, the whole string will be returned (an array with only one item).
  • limit: This parameter is optional. It specifies the integer that specifies the number of splits, items beyond the split limit will be excluded from the array.

Return value: It returns a new array, having split items. 

Example 1: This example splits the date by using (‘ ‘) and then takes the value at index = 0 of the array returned after split using split() method

Javascript




let d = '12/05/2019 12:00:00 AM';
 
console.log(d.split(' ')[0]);


Output

12/05/2019

Example 2: This example is quite similar to the previous one. This example first splits the date by using (‘ ‘) and then take the value at index = 0 of the array returned after split using split() method. It works with Date objects. 

Javascript




let d = new Date();
         
console.log(d.toISOString().split('T')[0]);


Output

2023-06-14

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads