How to remove time from date using JavaScript ? - GeeksforGeeks

22 Feb, 2022
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:
jayjbm94
javascript-date
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
How to get character array from string in JavaScript?
Remove elements from a JavaScript Array
How to get selected value in dropdown list using JavaScript ?
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?