-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
  • 33,375
  • 6
  • 36
  • 64
hussain
  • 6,587
  • 18
  • 79
  • 152

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,062
  • 1
  • 11
  • 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
  • 3,162
  • 2
  • 9
  • 19