How to remove time from date using JavaScript ?
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:
Recommended Posts:
- PHP | Date and Time
- How to get the current date and time in seconds?
- PHP | Spreadsheet | Setting a date and/or time value in a cell
- JavaScript | Date
- JavaScript | Date.now()
- Date.UTC( ) In JavaScript
- JavaScript | Date Formats
- JavaScript | Date.parse()
- JavaScript | date.setUTCMonth()
- JavaScript | date.toLocaleDateString()
- JavaScript | Set Date Methods
- Increment a given date in JavaScript
- JavaScript | date.toLocaleTimeString()
- JavaScript | Get Date Methods
- How to get the current date in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

