如何使用moment.js转换参数日期



我有一个值this.birthday,它包含这个值Mon Aug 06 2018 00:00:00 GMT-0500 (CDT)T00:00:00-00:00

我的生日模型收到这个值,即使我的日期选择器是这样配置的…

<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>

如何使用moment.js将此日期值转换为YYYY-MM-DD

我正试图将生日中的日期值操作为我想要的输出。。。。但我不知道怎么做。这些文件似乎都没有帮助。

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;
})
}

因为el-date-picker已经将Date对象设置为birthday。要将其转换为格式化的字符串,只需使用以下内容即可。

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

好的,这是我发现的。。。。我在网上随便找了一页。。。。https://github.com/ankurk91/vue-bootstrap-datetimepicker

里面有一条线索。。

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

这似乎是关键。我的birthday参数现在接收到正确的格式。

最新更新