Vue js datepicker, getting only formatted date portion

Viewed 1912

I have a working instance of Vue Datepicker, which is functional to the point of picking a date and logging it on select within the console.

The problem is that it logs as Fri Oct 18 2019 15:01:00 GMT-0400 but I need to send the formatted date portion of this like 2019-10-18 only.

This is vuejs-datepicker library and I can't seem to get anything to work with this:

customFormatter(date) {
  return moment(date).format('MMMM Do YYYY, h:mm:ss a');
}

What exactly am I doing wrong here?

<datepicker :value="date" @selected="CallDateFunction"></datepicker>

date(){
  return {
    date: '',
    ...

CallDateFunction(date){
  console.log(date);
}
3 Answers

vuejs-datepicker's selected callback is called with either a date object or null.

You can use the following example code to get a string representation of the date only:

CallDateFunction(date){
  if (date) {
    const dateString = date.toISOString().substring(0, 10);
    console.log(dateString);
  } else {
    console.log('null date');
  }
}

You can use the javascript function

jsref_toisostring

.

Documentation

Its pretty forward:

var d = new Date();
var n = d.toISOString();
Related