-2

I am trying to remove time from the date format which is coming from the backend , what is correct approach to achieve this task? Its not a date object.

main.js

const date = "2020-08-05 00:08:00 "
console.log(date.split[0];

should output (but it is not happening):

"2020-08-05"
Louys Patrice Bessette
  • 31,534
  • 6
  • 35
  • 61
hussain
  • 5,801
  • 15
  • 62
  • 132

2 Answers2

2

You should split by blank space and then get the first element, remember that split is a function.

const date = "2020-08-05 00:08:00 "
console.log(date.split(" ")[0]);
Álvaro Tihanyi
  • 1,043
  • 1
  • 9
  • 18
0

You could use regex as date format will be the same always.

const date = '2020-08-05 00:08:00 ';
const ret = date.match(/\d{4}-\d{2}-\d{2}/);
console.log(ret[0]);
mr hr
  • 2,954
  • 2
  • 8
  • 18