-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
  • 27,837
  • 5
  • 32
  • 57
hussain
  • 4,747
  • 12
  • 51
  • 120

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,011
  • 1
  • 7
  • 16
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,925
  • 2
  • 5
  • 17