简体   繁体   中英

how to convert paramater date with moment.js

I have a value this.birthday which containes this value Mon Aug 06 2018 00:00:00 GMT-0500 (CDT)T00:00:00-00:00

my birthday model receives this value even tho' my datepicker is configured like so...

<el-date-picker v-model="birthday" v-validate="'required|date_format:YYYY-MM-DD'" type="date" placeholder="yyyy-mm-dd" data-date-format="yyyy-MM-dd" :picker-options="pickerOptions1"></el-date-picker>

How can I use moment.js to convert this date value to YYYY-MM-DD ?

I'm trying to manipulate the date value held inside birthday to my desired output....but I have no clue how. None of the documentation seems to be helping.

onSubmitted() {
  axios.post(`${process.env.KITTY_URL}/api/v1/cats/`, {
    name: this.name,
    age: this.age,
    gender: this.gender,
    cat_type: this.cat_type,
    litter_mates: this.litter_mates,
    weight: this.weight,
    birthday: moment(this.birthday.toString()).format('DD-MM-YYYY'),<---?????? what should this be???
    weight_unit: this.weight_unit
  })
    .then(response => {
      console.log(response);
      response.status === 201 ? this.showSuccess = true : this.showDanger = true
    })
    .catch(error => {
      console.log(error);
      this.showDanger = true;
    })
}

Since the el-date-picker is already setting a Date object to birthday . To convert it to a formatted string, just use the following and it should work.

moment(this.birthday).format('YYYY-MM-DD')

Ok here is what I found....looking into the internet I found this random page.... https://github.com/ankurk91/vue-bootstrap-datetimepicker

within it was a clue..

// ...
options: {
  format: 'DD/MM/YYYY',
  useCurrent: false,
}

this seemed to be the key. my birthday param now receives the correct format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM