vue之moment的使用方式

Samira ·
更新时间:2024-09-21
· 1200 次阅读

目录

前言

一、moment是什么?

二、使用步骤(例:默认查询时间24小时之前~当前时间)

三、日期格式 

四、new Date() 相关

前言

在日常开发中,我们常常会遇到以下几种场景:

需要对日期进行非标准格式展示,如 :2021年5月11日星期二下午6点42分

需要对日期进行处理,如:要取前24小时的时间 等

在这时候用js原生的new Date()处理就有些麻烦了,因此我们找到了moment这个类库

一、moment是什么?

moment 是一个 JavaScript 日期处理类库。

注:以下所有时间相对于现在时间:2021/05/11/18:42 星期二

1.日期格式化:

moment().format('MMMM Do YYYY, h:mm:ss a'); // 五月 11日 2021, 6:42:31 下午 moment().format('dddd'); // 星期二 moment().format("MMM Do YY"); // 5月 11日 21 moment().format('YYYY [escaped] YYYY'); // 2021 escaped 2021 moment().format(); //2021-05-11T18:06:42+08:00

2.相对时间:

moment("20111031", "YYYYMMDD").fromNow(); // 2011/10/31号相对于现在是: 10 年前 moment("20120620", "YYYYMMDD").fromNow(); // 2012/06/20号相对于现在是: 9 年前 moment().startOf('day').fromNow(); //当前日期开始即:2021/05/11/00:00:00相对于现在是: 19 小时前 moment().endOf('day').fromNow(); //当前日期结束即:2021/05/11/24:00:00相对于现在是: 5 小时内 moment().startOf('hour').fromNow(); //当前日期小时开始即:2021/05/11/18:00:00相对于现在是: 42分钟前

3.日历时间:

moment().subtract(10, 'days').calendar(); // 当前时间往前推10天的日历时间: 2021/05/01 moment().subtract(6, 'days').calendar(); // 当前时间往前推6天: 上星期三18:42 moment().subtract(3, 'days').calendar(); // 当前时间往前推3天: 上星期六18:42 moment().subtract(1, 'days').calendar(); // 当前时间往前推1天: 昨天18:42 moment().calendar(); // 今天18:42 moment().add(1, 'days').calendar(); // 当前时间往后推1天: 明天18:42 moment().add(3, 'days').calendar(); // 当前时间往后推3天: 下星期五18:42 moment().add(10, 'days').calendar(); // 当前时间往后推10天: 2021/05/21

4.多语言支持:

moment.locale(); // zh-cn moment().format('LT'); // 18:42 moment().format('LTS'); // 18:42:31 moment().format('L'); // 2021/05/11 moment().format('l'); // 2021/5/11 moment().format('LL'); // 2021年5月11日 moment().format('ll'); // 2021年5月11日 moment().format('LLL'); // 2021年5月11日下午6点42分 moment().format('lll'); // 2021年5月11日 18:42 moment().format('LLLL'); // 2021年5月11日星期二下午6点42分 moment().format('llll'); // 2021年5月11日星期二 18:42 二、使用步骤(例:默认查询时间24小时之前~当前时间)

1.引入库

$ npm install moment --save

2.在main.js中全局引入(也可单独在使用的文件中引入,具体看需求)

import moment from "moment" Vue.prototype.$moment = moment;

3.在需要使用日期的地方使用

HTML中:

 <el-date-picker         v-model="timeRange"         type="datetimerange"         range-separator="至"         start-placeholder="开始日期"         end-placeholder="结束日期">  </el-date-picker>

JS中:

 data() {       return {          timeRange:[],       }    },   mounted(){         let start = this.$moment()             .subtract('1', 'd')             .format('YYYY-MM-DD HH:mm:ss') //当前时间往前推1天(24小时):2021-05-10 18:42:53         let end = this.$moment().format('YYYY-MM-DD HH:mm:ss') //当前时间:2021-05-11 18:42:53         this.timeRange=[start,end]    },   三、日期格式  格式含义举例备注
yyyy2021同YYYY
M1不补0
MM01 
d2不补0
dd02 
dddd星期星期二 
H小时324小时制;不补0
HH小时1824小时制
h小时312小时制,须和 A 或 a 使用;不补0
hh小时0312小时制,须和 A 或 a 使用
m分钟4不补0
mm分钟04 
s5不补0
ss05 
AAM/PMAM仅 format 可用,大写
aam/pmam仅 format 可用,小写

具体方法以及参数可详见moment官方文档

四、new Date() 相关

日期都写这么多了,那new Date()也一起总结下吧

let time = new Date(); //获取当前时间 Tue May 11 2021 18:42:51 GMT+0800 (中国标准时间) let year = time.getFullYear(); //获取年 2021 let month = time.getMonth() + 1; //获取月 5 let day = time.getDate(); //获取天 11 let h = time.getHours(); //获取小时 18 let m = time.getMinutes(); //获取分钟 42 let s = time.getSeconds(); //获取秒 51 let weekDay = time.getDay(); //获取星期 2

以上为个人经验,希望能给大家一个参考,也希望大家多多支持软件开发网。



VUE moment

需要 登录 后方可回复, 如果你还没有账号请 注册新账号