Horje
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


Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to add a parameter to the URL in JavaScript ? How to add a parameter to the URL in JavaScript ?
How to write an inline IF statement in JavaScript ? How to write an inline IF statement in JavaScript ?
How to get a list of associative array keys in JavaScript ? How to get a list of associative array keys in JavaScript ?
How to center a popup window on screen? How to center a popup window on screen?
JavaScript Check if a key exists inside a JSON object JavaScript Check if a key exists inside a JSON object

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
8