zoukankan      html  css  js  c++  java
  • Moment.js简单使用

    1、设置语言环境,如设置中文环境:

    moment.locale("zh-cn");

    2、当前时间、指定时间:

    复制代码
    // 假设当前时间为:2018年12月10日
    
    moment(); // 结果:moment("2018-12-10T14:25:00.463")
    moment(new Date()); // 结果:moment("2018-12-10T14:25:00.463")
    
    moment("2018-12-10 13:48:36.458"); // 结果:moment("2018-12-10T13:48:36.458")
    复制代码

    3、年月日:

    复制代码
    // 假设当前时间为:2018年12月10日
    
    //
    moment().format("YY");  // 结果:18
    moment().format("YYYY"); // 结果:2018
    moment().format("gg"); // 结果:18
    moment().format("gggg"); // 结果:2018
    moment().format("GG"); // 结果:18
    moment().format("GGGG"); // 结果:2018
    
    //
    moment().format("M"); // 结果:12
    moment().format("MM"); // 结果:12
    moment().format("Mo"); // 结果:12月
    moment().format("MMM"); // 结果:12月
    moment().format("MMMM"); // 结果:十二月
    
    //
    moment().format("D"); // 结果:10
    moment().format("Do"); // 结果:10日
    moment().format("DD"); // 结果:10
    复制代码

    4、时分秒:

    复制代码
    // 假设当前时间为:2018年12月10日
    
    // 12小时制
    moment().format("h"); // 结果:2
    moment().format("hh"); // 结果:02
    // 24小时制
    moment().format("H"); // 结果:14
    moment().format("HH"); // 结果:14
    
    //
    moment().format("m"); // 结果:55
    moment().format("mm"); // 结果:55
    
    //
    moment().format("s"); // 结果:5
    moment().format("ss"); // 结果:05
    
    // 毫秒
    moment().format("S"); // 结果:8
    moment().format("SS"); // 结果:87
    moment().format("SSS"); // 结果:876
    复制代码

    5、显示周几、星期几:

    复制代码
    // 显示当前周几下标,0:周日、1:周一、2:周二、...、周六:6
    moment().format("d"); // 结果:1
    
    // 显示当前是周几,如:周一、周二、周三、...
    moment().format("ddd"); // 结果:周一
    
    // 显示当前是星期几,如:星期一、星期二、星期三、...
    moment().format("dddd"); // 结果:星期三
    复制代码

    6、显示季度:

    // 假设当前时间为:2018年12月10日
    
    moment().format("Q"); // 结果:4

    7、显示一年中的第几天:

    // 假设当前时间为:2018年12月10日
    
    moment().format("DDD"); // 结果:344
    moment().format("DDDo"); // 结果:344日
    moment().format("DDDD"); // 结果:344

    8、星期下标:

    复制代码
    // 假设当前时间为:2018年12月10日
    
    // Locale:0 1 ... 5 6
    moment().format("e"); // 结果:0
    // ISO:1 2 ... 6 7
    moment().format("E"); // 结果:1
    复制代码

    9、一年中第几个星期:

    复制代码
    // 假设当前时间为:2018年12月10日
    
    moment().format("w"); // 结果:50
    moment().format("wo"); // 结果:50周
    moment().format("ww"); // 结果:50
    
    moment().format("W"); // 结果:50
    moment().format("Wo"); // 结果:50周
    moment().format("WW"); // 结果:50
    复制代码

    10、上午还是下午:

    // 假设当前时间为:2018年12月10日
    
    moment().format("A"); // 结果:下午
    moment().format("a"); // 结果:下午

    11、时间戳、毫秒数:

    // 时间戳(总秒数)
    moment().format("X"); // 结果:1544425410
    // 总毫秒数
    moment().format("x"); // 结果:1544425410853

     12、时间差:

    // 默认时间差为毫秒
    let begin = moment();
    let end = moment().add(2,'second');
    end.diff(begin); // 结果:2000
  • 相关阅读:
    javascript闭包函数
    取消后续内容执行
    vs安装失败,发生严重错误,错误号:Error 0x80070643
    ref
    深入类的方法
    学习过程中的三个小小程序
    SQL Server 中存储过程的练习
    SQL Server系统存储过程
    SQL-server的事务,视图和索引
    用C#,SQL Server编写的音乐播放软件
  • 原文地址:https://www.cnblogs.com/ivday/p/12573035.html
Copyright © 2011-2022 走看看