Skip to content
Related Articles

Related Articles

How to remove time from date using JavaScript ?
  • Last Updated : 30 May, 2019

Given a Date Object and the task is to remove the time portion of the object using 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 the splitted items.



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




<!DOCTYPE HTML> 
<html
    <head
        <title
            Remove time from date
        </title>     
    </head
      
    <body style = "text-align:center;">
           
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
          
        <p id = "GFG_UP" style
            "font-size: 15px; font-weight: bold;">
        </p>
          
        <button onclick = "GFG_Fun()">
            click here
        </button>
          
        <p id = "GFG_DOWN" style =
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            var up = document.getElementById('GFG_UP');
            var d = '12/05/2019 12:00:00 AM';
            up.innerHTML = d;
            var down = document.getElementById('GFG_DOWN'); 
              
            function GFG_Fun() {
                down.innerHTML = d.split(' ')[0];;
            }
        </script
    </body
</html>                    

Output:

  • Before clicking on the button:
  • After clicking on the button:

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.




<!DOCTYPE HTML> 
<html
    <head
        <title
            Remove time from date
        </title>     
    </head
      
    <body style = "text-align:center;">
           
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
          
        <p id = "GFG_UP" style
            "font-size: 15px; font-weight: bold;">
        </p>
          
        <button onclick = "GFG_Fun()">
            click here
        </button>
          
        <p id = "GFG_DOWN" style
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            var up = document.getElementById('GFG_UP');
            var d = new Date();
            up.innerHTML = d;
            var down = document.getElementById('GFG_DOWN'); 
              
            function GFG_Fun() {
                down.innerHTML = d.toISOString().split('T')[0];
            }
        </script
    </body
</html>                    

Output:

  • Before clicking on the button:
  • After clicking on the button:
My Personal Notes arrow_drop_up
Recommended Articles
Page :