Day.js 使用记录

安装

1
npm install dayjs

引入

1
import * as dayjs from 'dayjs'

基本用法

当前时间

1
dayjs() // === dayjs(new Daye())

格式化

根据传入的占位符返回格式化后的日期。

1
dayjs('2022/2/16').format('YYYY-MM-DD') // '2022-2-16' 

差异

返回指定单位下两个日期时间之间的差异,默认返回毫秒为单位的差异。

第二个参数指定单位,第三个参数为 true 时返回浮点数。

1
2
const date1 = dayjs('2019-01-25')
date1.diff('2018-06-05', 'month', true) // 7.645161290322581

可以用来计算日期到日期一共多少天:

1
2
3
const calculateTime = (date1, date2) => {
return dayjs(date2).diff(dayjs(date1), 'day') + 1
}

时间的开始以及结束

返回复制的 Day.js 对象,并设置到一个时间的开始或者结束。

1
2
dayjs().startOf('day').format('YYYY/MM/DD HH:mm:ss') // 2023/02/22 00:00:00
dayjs().endOf('month').format('YYYY/MM/DD HH:mm:ss') // 2023/02/28 23:59:59

实践

返回 N 个月前的时间段或当月的时间段:

1
2
3
4
5
const getMonthArray = (n: number = 0) => {
const start = dayjs().subtract(n, 'month').startOf('month').valueOf()
const end = n === 0 ? dayjs().valueOf() : dayjs().subtract(n, 'month').endOf('month').valueOf()
return [start, end]
}

Vue3 + Vite 打包部署空白页

部署到宝塔后,首先根目录是可以正常访问的,但是将文件放在类似于 /about 这样的子目录就会白屏,首先考虑了一下是静态文件的路径出错了,检查了一下,默认是指向 / 而不是 ./,导致路径出错。

打开工程根目录下的 vite.config.ts,那就修改一下默认的 base

1
2
3
4
5
export default defineConfig({  
...,
base: './',
...
})

问题解决了,但页面依然是空白的……

那接着考虑一下是否是 Vue Router 有没有配置错误,目前使用的是 history 模式,可能是由于宝塔这边的 nginx 没有做相应的配置所导致的白屏,那就修改为 hash 模式。

修改 router/index.ts,将 createWebHistory 修改为 createWebHashHistory

1
2
3
4
5
const router = createRouter({  
history: createWebHashHistory(),
routes
})
export default router

重新部署一下,问题解决 ( ̄︶ ̄*)),多了丑陋的 #,但不用另外配置 nginx