zoukankan      html  css  js  c++  java
  • Moment.js的常用方法


    Moment.js的常用方法


    安装与引用
    官方文档

    安装

    npm install moment
    引用

    var moment = require('moment');


    自定义配置
    获取当前语言

    moment.locale();
    加载语言

    moment.locale('zh-cn');


    UTC
    获取UTC

    moment().utc();
    UTC偏移量

    moment().utcOffset();
    设置偏移量

    以下是相同的

    moment().utcOffset("+08:00");
    moment().utcOffset(8);
    moment().utcOffset(480);


    Moment和Date

    相互转换
    Date ==> Moment

    moment(new Date())


    Moment ==> Date

    moment().toDate()


    是否 Moment 对象
    moment.isMoment() // false
    moment.isMoment(new Date()) // false
    moment.isMoment(moment()) // true


    是否 Date 对象
    moment.isDate(); // false
    moment.isDate(new Date()); // true
    moment.isDate(moment()); // false


    验证日期格式是否正确
    moment("not a real date").isValid(); // false


    初始化日期
    字符串
    var day = moment("1995-12-25");
    支持以下格式

    2013-02-08T09 # An hour time part separated by a T
    2013-02-08 09 # An hour time part separated by a space
    2013-02-08 09:30 # An hour and minute time part
    2013-02-08 09:30:26 # An hour, minute, and second time part
    2013-02-08 09:30:26.123 # An hour, minute, second, and millisecond time part
    2013-02-08 24:00:00.000 # hour 24, minute, second, millisecond equal 0 means next day at midnight


    字符串+格式化
    moment("12-25-1995", "MM-DD-YYYY");


    对象方式
    moment({ hour:15, minute:10 });
    moment({ y:2010, M:3, d:5, h:15, m:10, s:3, ms:123});
    moment({ year :2010, month :3, day :5, hour :15, minute :10, second :3, millisecond :123});
    moment({ years:2010, months:3, days:5, hours:15, minutes:10, seconds:3, milliseconds:123});
    moment({ years:2010, months:3, date:5, hours:15, minutes:10, seconds:3, milliseconds:123});


    Unix 偏移量(毫秒)
    var day = moment(1318781876406);
    取值

    moment().valueOf();


    Unix 时间戳(秒)
    var day = moment.unix(1318781876);
    取值

    moment().unix();


    Date对象
    var day = new Date(2011, 9, 16);
    var dayWrapper = moment(day);


    数组
    moment([2010, 1, 14, 15, 25, 50, 125]);
    moment([2010]);
    moment([2010, 6]);
    moment([2010, 6, 10]);


    取值/赋值
    日期和时间
    // 毫秒
    moment().millisecond(Number);
    moment().millisecond(); // Number
    moment().milliseconds(Number);
    moment().milliseconds(); // Number
    // 秒
    moment().second(Number);
    moment().second(); // Number
    moment().seconds(Number);
    moment().seconds(); // Number
    // 分钟
    moment().minute(Number);
    moment().minute(); // Number
    moment().minutes(Number);
    moment().minutes(); // Number
    // 小时
    moment().hour(Number);
    moment().hour(); // Number
    moment().hours(Number);
    moment().hours(); // Number
    // 日期
    moment().date(Number);
    moment().date(); // Number
    moment().dates(Number);
    moment().dates(); // Number


    年月日时分秒
    // 取值
    moment().get('year');
    moment().get('month'); // 0 to 11
    moment().get('date');
    moment().get('hour');
    moment().get('minute');
    moment().get('second');
    moment().get('millisecond');
    // 赋值
    moment().set('year', 2013);
    moment().set('month', 3); // April
    moment().set('date', 1);
    moment().set('hour', 13);
    moment().set('minute', 20);
    moment().set('second', 30);
    moment().set('millisecond', 123);

    moment().set({'year': 2013, 'month': 3});


    星期的取值和赋值
    周日为0

    周六为6

    moment().day(-7); // last Sunday (0 - 7)
    moment().day(7); // next Sunday (0 + 7)
    moment().day(10); // next Wednesday (3 + 7)
    moment().day(24); // 3 Wednesdays from now (3 + 7 + 7 + 7)
    按区域标准

    // 比如周一是一星期的第一天
    moment().weekday(-7); // last Monday
    moment().weekday(7); // next Monday

    时间操作
    Key Shorthand
    years y
    quarters Q
    months M
    weeks w
    days d
    hours h
    minutes m
    seconds s
    milliseconds ms


    加法

    moment().add(7, 'days').add(1, 'months'); // with chaining
    moment().add(7, 'd');
    moment().add({days:7,months:1}); // with object literal


    减法

    moment().subtract(7, 'days');


    显示
    比如

    moment().format(); // "2014-09-08T08:02:17-05:00" (ISO 8601)
    moment().format("YYYY-MM-DD hh:mm:ss"); // "2014-09-08 02:17:10"
    moment().format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm"
    moment().format("ddd, hA"); // "Sun, 3PM"
    moment('gibberish').format('YYYY MM DD'); // "Invalid date"
    排除字符

    moment().format('[today] dddd'); // 'today Sunday'

    比较
    //之前
    moment('2010-10-20').isBefore('2010-10-21'); // true
    //相同
    moment('2010-10-20').isSame('2010-10-20'); // true
    //之后
    moment('2010-10-20').isAfter('2010-10-19'); // true
    //之间
    moment('2010-10-20').isBetween('2010-10-19', '2010-10-25'); // true
    // 是否闰年
    moment().isLeapYear();


    对象克隆
    var a = moment([2012]);
    var b = moment(a);
    a.year(2000);
    b.year(); // 2012
    或者

    var a = moment([2012]);
    var b = a.clone();
    a.year(2000);
    b.year(); // 2012

  • 相关阅读:
    vue中Axios的封装和API接口的管理
    如何配置Webpack/Vue-CLI实现前端跨域(附跨域大全)
    前端面试几个重要知识点
    js常用函数
    JS中的枚举和不可枚举
    可配置性属性和不可配置性属性
    Object.create()和深拷贝
    JavaScript 中 call()、apply()、bind() 的用法
    从深入到通俗:Object.prototype.toString.call()
    js原生实现三级联动下拉菜单
  • 原文地址:https://www.cnblogs.com/ranyonsue/p/13825080.html
Copyright © 2011-2022 走看看