1.vant中倒计时处理
效果
由于vant返回的就是HH: mm : ss 格式,所以切割就可以了,如果返回的是其他格式例如2021-4-17之类的,就得先加入new Date()中,再分别取出年月日切割.
<!-- 引入倒计时 -->
<van-count-down :time="time" format="HH: mm : ss ">
<template #default="timeData">
<span class="countDownBlock">{
{
timeData.hours | dateFormat("1")
}}</span>
<span class="countDownBlock">{
{
timeData.hours | dateFormat("2")
}}</span>
<span class="countDownColon">:</span>
<span class="countDownBlock">{
{
timeData.minutes | dateFormat("1")
}}</span>
<span class="countDownBlock">{
{
timeData.minutes | dateFormat("2")
}}</span>
<span class="countDownColon">:</span>
<span class="countDownBlock">{
{
timeData.seconds | dateFormat("1")
}}</span>
<span class="countDownBlock">{
{
timeData.seconds | dateFormat("2")
}}</span>
</template>
</van-count-down>
filters: {
// 过滤成单个时间,如果不满10,就加上0
dateFormat(value, type) {
let data = value.toString();
data = data < 10 ? "0" + data : data;
if (type == 1) {
let filterDate = data.slice(0, 1);
return filterDate;
} else {
let filterDate = data.slice(1, 2);
return filterDate;
}
},
2.格式化日期yyyy-MM
这里获得的格式就是Sat Apr 17 2021 17:12:46 GMT+0800 (中国标准时间)类型,如果不是,如上所述,需要先转化,例如:new Date(date)
formatTime(date) {
var YY = date.getFullYear();
var MM =
date.getMonth() + 1 < 10
? "0" + (date.getMonth() + 1)
: date.getMonth() + 1;
return YY + "-" + MM;
},
3.yyyy MM转为yyyy年MM月
spe切割的是标点符号,比如2021-04中的"-",2021.04中的"."
selectTime(value, spe) {
let date = value.split(spe);
return `${
date[0]}年${
date[1]}月`;
},
4.日期相加或相减获取截止时间
这里是传入年月日,返回数组形式的年月日,可以根据业务做调整
gettime(vyear, vmonth, vday) {
var date = new Date();
var nyear = date.getFullYear() + vyear; //获取年份差
var nmonth = date.getMonth() + vmonth; //获取月份差
var nday = date.getDate() + vday; //获取日差
var ux = Date.UTC(nyear, nmonth, nday); //转化为时间戳
var date1 = new Date(ux); //构造目标日期
return [date1.getFullYear(), date1.getMonth() + 1, date1.getDate()];
},
5.格式化时间yyyy-MM-dd hh:MM:ss
formatDate(date) {
var date = new Date(date);
var YY = date.getFullYear() + "-";
var MM =
(date.getMonth() + 1 < 10
? "0" + (date.getMonth() + 1)
: date.getMonth() + 1) + "-";
var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
var hh =
(date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
var mm =
(date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
":";
var ss =
date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
return YY + MM + DD + " " + hh + mm + ss;
},
6.根据某个时间判断是否在区间时间内
/**
* @method 根据时间判断问候语
*/
judgeTime() {
let time = [
{
start: "05:00",
end: "07:59",
timeToast: "早上好",
},
{
start: "08:00",
end: "11:59",
timeToast: "上午好",
},
{
start: "12:00",
end: "13:59",
timeToast: "中午好",
},
{
start: "14:00",
end: "18:59",
timeToast: "下午好",
},
{
start: "19:00",
end: "04:59",
timeToast: "晚上好",
},
];
this.timeToast = time.find((item) =>
this.isDuringDate(item.start, item.end)
).timeToast;
},
/**
* [isDuringDate 比较当前时间是否在指定时间段内]
* @DateTime 2021/05/26
* @version 1.0
* @param {String} beginDateStr 开始时间
* @param {String} endDateStr 结束时间
* @return Boolean
*/
isDuringDate(beginDateStr, endDateStr) {
let curDate = `${
new Date().getHours()}:${
new Date().getMinutes() < 10
? "0" + new Date().getMinutes()
: new Date().getMinutes()
}`;
return (
(curDate >= beginDateStr
? true
: beginDateStr == "19:00") &&
(curDate <= endDateStr
? true
: endDateStr == "04:59")
);
},