美文网首页
Moment.js方法总结

Moment.js方法总结

作者: Camilia_yang | 来源:发表于2019-11-30 09:50 被阅读0次

    moment() 得到一个moment对象
    moment对象的一些方法:
    .format() //日期的字符串

    默认值

    指定某个的unit来创建moment对象,其他未指定的unit就按照当下的时间填补
    moment() //2019-11-23T10:58:03+08:00
    moment({hour: 5, minute: 10, seconds: 20, milliseconds: 300}); // format后结果: 2019-11-23T05:10:20+08:00
    moment(5, "DD"); // (默认:本月)第5天 format后结果: 2019-11-05T00:00:00+08:00
    moment(3,'MM') // (默认:本年)3月 format后结果: 2019-03-01T00:00:00+08:00
    moment("4 4 05:06:07", "MM DD hh:mm:ss"); // 第二个参数表明第一个参数的格式,因此如果第一个参数的不符合第二个参数表示的格式,会解析出错 ;format后结果:2019-04-04T05:06:07+08:00

    取值/赋值

    根据官方文档:
    Moment.js uses overloaded getters and setters. You may be familiar with this pattern from its use in jQuery.
    Calling these methods without parameters acts as a getter, and calling them with a parameter acts as a setter.

    取值

    取值一般返回的都是具体的数值(Number),赋值一般返回的是moment()对象
    取值之秒/分/时/日/月/年:
    moment().millisecond() //Number
    moment().second(); // Number
    moment().minute(); // Number
    moment().hour(); // Number
    moment().day(); // 星期
    moment().date(); // 日期
    moment().month(); // Number 【和new Date().getMonth()一样,比实际月份少一个月...】
    moment().year(); // Number
    moment().dayOfYear() //一年中的第几天

    赋值

    当下时间:moment()//2019-11-23T11:45:10+08:00

    moment().seconds(30) === new Date().setSeconds(30);
    moment().second(Number);//设置秒
    moment().minute(Number); // 设置分
    moment().hour(Number); //设置小时
    moment().date(Number);//设置日期 Number为1-31的数字
    moment().date(7); //format后结果:2019-11-07T11:45:10+08:00
    moment().day(Number/String) //设置【星期】
    //Number--正:本周或本周之后(大于7)负:上周或上周之前(<-7);
    //String--"Sunday" ,"Monday"ect

    moment().weekday(Number) //设置本地化星期
    moment().weekday()详解:
    星期 (区域标准)2.1.0+

    Gets or sets the day of the week according to the locale.
    If the locale assigns Monday as the first day of the week, moment().weekday(0) will be Monday. If Sunday is the first day of the week, moment().weekday(0) will be
    Sunday.

    语法:
    moment().weekday(Number);
    moment().weekday(); // Number

    // when Monday is the first day of the week
    moment().weekday(-7); // last Monday
    moment().weekday(7); // next Monday
    // when Sunday is the first day of the week
    moment().weekday(-7); // last Sunday
    moment().weekday(7); // next Sunday
    

    设置一年中的第几天:
    moment().dayOfYear(Number);
    设置一年中的第几个星期:
    moment().week(Number);

    取值也可以用get()方法:
    moment().get(unit) === moment()unit
    赋值也可以用set()方法:
    moment().set(String, Int);
    moment().set(Object(String, Int));

    最大值

    返回两个moment对象中更大的日期:
    moment.max(Moment[,Moment...])
    最小值:moment.min(Moment[,Moment...]);

    显示

    操作

    • startOf() 设置(单位)...的第一...
    • endOf() 设置(单位)...的最后一...
      moment().startOf('month'); // set to the first of this month, 12:00 am

    相关文章

      网友评论

          本文标题:Moment.js方法总结

          本文链接:https://www.haomeiwen.com/subject/mythwctx.html