如何在vue中使用moment

发布时间:2021-05-13 15:56:03 作者:Leah
来源:亿速云 阅读:790

如何在vue中使用moment?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

一、moment是什么?

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

安装 moment

如果之前安装过就不用再安装了。

npm install moment -- save

注:以下所有时间相对于现在时间: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是什么

Vue是一套用于构建用户界面的渐进式JavaScript框架,Vue与其它大型框架的区别是,使用Vue可以自底向上逐层应用,其核心库只关注视图层,方便与第三方库和项目整合,且使用Vue可以采用单文件组件和Vue生态系统支持的库开发复杂的单页应用。

关于如何在vue中使用moment问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. 如如何使用journalctl命令?
  2. moment常用方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue moment

上一篇:使用Redis怎么实现延迟队列

下一篇:如何在Python中使用Blending算法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》