vue项目如何安装使用moment.js
1、什么是moment.js
它是一个简单易用的轻量级javascript日期处理类库,提供了日期格式化、日期解析等功能,且支持在浏览器和nodejs两种环境中运行
2、vue项目中安装
npm install moment --save
3、导入
在main.js中导入该组件
import moment from 'moment'//导入文件 moment.locale('zh-cn');//需要汉化 vue.prototype.$moment = moment;//赋值使用
4、用法
this.moment()
- 本季度
const start = moment().startof('quarter').format('yyyy-mm-dd') // 开始 const end = moment().endof('quarter').format('yyyy-mm-dd') // 结束
- 上个季度
const start = moment().quarter(moment().quarter() - 1).startof('quarter').format('yyyy-mm-dd') const end = moment().quarter(moment().quarter() - 1).endof('quarter').format('yyyy-mm-dd')
- 本周
const start = moment().startof('week') .add(1, 'day').format('yyyy-mm-dd hh:mm:ss') const end = moment().endof('week').add(1, 'day').format('yyyy-mm-dd hh:mm:ss')
- 上周
const start = moment().subtract(weekofday + 7 - 1, 'days') .format('yyyy-mm-dd') const end = moment().subtract(weekofday, 'days') .format('yyyy-mm-dd')
- 本年
const start = moment() .year(moment().year()) .startof('year') .format('yyyy-mm-dd') const end = moment() .year(moment().year()) .endof('year').format('yyyy-mm-dd')
- 去年
const start = moment().year(moment().year() - 1).startof('year') .format('yyyy-mm-dd') const end = moment() .year(moment().year() - 1) .endof('year') .format('yyyy-mm-dd')
- 今天
const start=moment().startof('day').format('yyyy-mm-dd hh:mm:ss') const end=moment().endof('day').format('yyyy-mm-dd hh:mm:ss')
- 昨天
const start= moment().subtract('days',1).startof('days').format('yyyy-mm-dd hh:mm:ss') const end=moment().subtract('days',1).endof('days').format('yyyy-mm-dd hh:mm:ss')
- 某一日期前一天后一天
starttime = moment(starttime).subtract(1,"days").format("yyyy-mm-dd");//前一天 endtime = moment(endtime).add(1,"days").format("yyyy-mm-dd");//后一天
- 当前月份的第一天&最后一天
moment().startof('month').format("yyyy-mm-dd") moment().endof('month').format("yyyy-mm-dd"),
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论