Moment.js方法总结

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,222评论 0 13
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,186评论 0 10
  • Moment.js常见用法总结 Moment.js是一个轻量级的JavaScript时间库,它方便了日常开发中对时...
    zhouqichao阅读 166,210评论 8 90
  • 166钱梦琳14号 大数据”是目前互联网时代的主要特征,它影响了人们的生活方式,改变了企业的决策方式,转变了现代社...
    BlancheMeng阅读 3,887评论 0 0
  • 聊一聊 敏捷管理 每种管理的方式都有边界 在实际的管理de过程中却有相通之处 其实敏捷开发是一种过程控制论,通俗的...
    后龙山大掌柜阅读 2,593评论 0 2

友情链接更多精彩内容