uniapp時間格式化處理
應用需求分析:前臺頁面有時需要展示YYYY-MM-DD格式,但后臺卻返回給我們YYYY-MM-DD hh:mm:ss、或者是一串字符
//格式化處理 方式一:
dateFormat(time) {
let date = new Date(time);
let year = date.getFullYear();
// 在日期格式中,月份是從0開始的,因此要加0,使用三元表達式在小于10的前面加0,以達到格式統一 如 09:11:05
let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
// 拼接
// return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
return year + "-" + month + "-" + day;
},
<view v-if="item.rukudate">{{ dateFormat(item.rukudate) }}</view>
或者
<view v-if="item.yuyuedate" :class="dateFormat(item.yuyuedate) == day ? 'sameDay' : '' ">{{ dateFormat(item.yuyuedate) }}</view>
//格式化處理 方式二:
// 時間過濾器
filters:{
formatDate(date){
console.log(date)
let newDate = new Date(date);
let year = newDate.getFullYear();
let month = newDate.getMonth().toString().padStart(2,0);
let day = newDate.getDay().toString().padStart(2,0);
return year + '-' + month + '-' + day;
}
},
<view>發表時間:{{ item.add_time | formatDate }}</view>
Element UI -- el-date-picker 日期組件設置默認值
<el-date-picker
style="width:23%"
v-model="time"
type="daterange"
range-separator="至"
start-placeholder="開始日期"
end-placeholder="結束日期"
value-format="yyyy-MM-dd"
></el-date-picker>
// 默認時間
timeDefault () {
let date = new Date()
// 通過時間戳計算
let defalutStartTime = date.getTime() - 7 * 24 * 3600 * 1000 // 轉化為時間戳
let defalutEndTime = date.getTime()
let startDateNs = new Date(defalutStartTime)
let endDateNs = new Date(defalutEndTime)
// 月,日 不夠10補0
defalutStartTime = startDateNs.getFullYear() + '-' + ((startDateNs.getMonth() + 1) >= 10 ? (startDateNs.getMonth() + 1) : '0' + (startDateNs.getMonth() + 1)) + '-' + (startDateNs.getDate() >= 10 ? startDateNs.getDate() : '0' + startDateNs.getDate())
defalutEndTime = endDateNs.getFullYear() + '-' + ((endDateNs.getMonth() + 1) >= 10 ? (endDateNs.getMonth() + 1) : '0' + (endDateNs.getMonth() + 1)) + '-' + (endDateNs.getDate() >= 10 ? endDateNs.getDate() : '0' + endDateNs.getDate())
return [defalutStartTime, defalutEndTime]
}
————————————————
版權聲明:本文為CSDN博主「tiankongxiao」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/tiankongxiao/article/details/126444652
浙公網安備 33010602011771號