我正在使用 API 做我的專案,回應中的默認生日是YYYY-MM-DD但我想在我的模式對話框中以這種格式DD-MM-YYYY格式化和顯示生日,

這是代碼
axios
.post("test/test).
.then((response) => {
if (response.data.patients.length === 0) {
this.showAlert(this.$t("tess"), "error");
} else {
this.dataLoad = response.data.patients[0].patient;
console.log(this.dataLoad);
}
})
我將回應保存在dataLoad中,并想在模式對話框中顯示格式DD-MM-YYYY
<v-row>
<v-col cols="4">Birthdate</v-col>
<v-col cols="1">:</v-col>
<v-col cols="7">{{
this.dataLoad && this.dataLoad.birthdate
}}</v-col>
</v-row>
uj5u.com熱心網友回復:
呼叫以下函式。
function formatDate(dateStr)
{
var parts = dateStr.split("-");
return parts[2] "-" parts[1] "-" parts[0];
}
或者,您可以按如下方式使用它。
<v-row>
<v-col cols="4">Birthdate</v-col>
<v-col cols="1">:</v-col>
<v-col cols="7">{{
this.dataLoad && this.dataLoad.birthdate.split("-").reverse().join("-")
}}</v-col>
</v-row>
uj5u.com熱心網友回復:
您可以首先準備像您想要的格式一樣的出生日期,然后您可以將其加載到您的組件中:
new Vue({
el: '#vueRoot',
data: {birthDate: '1963-11-22'},
computed:{
birthDateDdmm(){
return new Date(this.birthDate 'T00:00:00Z')
.toLocaleDateString('en-GB',{timeZone:'UTC'})
},
birthDateAuto(){
return new Date(this.birthDate 'T00:00:00Z')
.toLocaleDateString([],{timeZone:'UTC'})
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id='vueRoot'>
<h1>input => {{birthDate}}</h1>
<h1>dd-MM-yy => {{birthDateDdmm}}</h1>
<h1>respect user prefs => {{birthDateAuto}}</h1>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/461777.html
標籤:Vue.js Vuetify.js 日期格式
