使用 day.js,使用 npm 安装:
npm install dayjs --save
1.执行安装依赖
//执行安装依赖
npm install dayjs --save
2.引入分为两种方法
第一个是在单文件里导入xxx.vue里面引入
import dayjs from 'dayjs'
第二个全局引入是在main.js里引用
//main.js引用
import dayjs from 'dayjs'
Vue.prototype.dayjs = dayjs// 全局使用dayjs
3.获取当前时间格式化
//获取当前时间格式化
let date = dayjs().format('YYYY-MM-DD') //年月日
let time = dayjs().format('HH:mm:ss'); //时间
4.搭配后端接口使用,将时间格式化传给后端,即可获取需要的数据
//搭配后端接口使用
let timeBegin = `${date} ${time}`
xxx接口({
id: id,
time: timeBegin ,//将时间格式化传给后端
}).then((res) => {
console.log(res) //即可获取需要的数据
})
作者上一篇文章,