HomeLanguagesJavascriptHow to remove time from date using JavaScript ?

How to remove time from date using JavaScript ?

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
RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS