-2

I have a simple problem. i need to get only the date as string and remove its time. How can i do this? I tried new Date() but its not working.

const value = '2018-04-09 00:00:00'
Joseph
  • 4,309
  • 11
  • 37
  • 87
  • 1
    Uh, just use bog standard string methods to chop off the time? Why wouldn't that work? – VLAZ May 09 '19 at 07:10

2 Answers2

1

You can try this

const currentDate = new Date();
const formattedDate = ''
  + currentDate.getDate().toString().padStart(2, '0') + '-'
  + (currentDate.getMonth() + 1).toString().padStart(2, '0') + '-'
  + currentDate.getFullYear();

console.log(formattedDate)

// output
"09-05-2019"
-1

function yyyymmdd(date) {
  var mm = date.getMonth() + 1; // getMonth() is zero-based
  var dd = date.getDate();

  return [date.getFullYear(),
          (mm>9 ? '' : '0') + mm,
          (dd>9 ? '' : '0') + dd
         ].join('-');
};

let date=new Date()
console.log(yyyymmdd(date))
Shiva
  • 175
  • 6