vue3显示今天明天日期以及日期差值

182 阅读1分钟
效果

44.png

在utils文件夹下创建format_date.js文件
//需要先下载
import dayjs from "dayjs";
//计算今明日期
export function formatMonthDay(date) {
  return dayjs(date).format("MM月DD日");
}
//计算日期差
export function getDiffDate(startDate, endDate) {
  return dayjs(endDate).diff(startDate, "day");
}
使用
import { formatMonthDay,getDiffDate } from "@/utils/format_date";
import { ref } from "vue";
// 获取今天startDate和明天endDate时间
const nowDate = new Date();
const newDate = new Date().setDate(nowDate.getDate() + 1);
//今天
const startDate = ref(formatMonthDay(nowDate));
//明天
const endDate = ref(formatMonthDay(newDate));
//日期差
const stayCount = ref(getDiffDate(nowDate, newDate));