Moment.js方法總結(jié)

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

默認值

指定某個的unit來創(chuàng)建moment對象,其他未指定的unit就按照當下的時間填補
moment() //2019-11-23T10:58:03+08:00
moment({hour: 5, minute: 10, seconds: 20, milliseconds: 300}); // format后結(jié)果: 2019-11-23T05:10:20+08:00
moment(5, "DD"); // (默認:本月)第5天 format后結(jié)果: 2019-11-05T00:00:00+08:00
moment(3,'MM') // (默認:本年)3月 format后結(jié)果: 2019-03-01T00:00:00+08:00
moment("4 4 05:06:07", "MM DD hh:mm:ss"); // 第二個參數(shù)表明第一個參數(shù)的格式,因此如果第一個參數(shù)的不符合第二個參數(shù)表示的格式,會解析出錯 ;format后結(jié)果:2019-04-04T05:06:07+08:00

取值/賦值

根據(jù)官方文檔:
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.

取值

取值一般返回的都是具體的數(shù)值(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的數(shù)字
moment().date(7); //format后結(jié)果:2019-11-07T11:45:10+08:00
moment().day(Number/String) //設置【星期】
//Number--正:本周或本周之后(大于7)負:上周或上周之前(<-7);
//String--"Sunday" ,"Monday"ect

moment().weekday(Number) //設置本地化星期
moment().weekday()詳解:
星期 (區(qū)域標準)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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,894評論 0 13
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,817評論 0 10
  • Moment.js常見用法總結(jié) Moment.js是一個輕量級的JavaScript時間庫,它方便了日常開發(fā)中對時...
    zhouqichao閱讀 166,346評論 8 90
  • 166錢夢琳14號 大數(shù)據(jù)”是目前互聯(lián)網(wǎng)時代的主要特征,它影響了人們的生活方式,改變了企業(yè)的決策方式,轉(zhuǎn)變了現(xiàn)代社...
    BlancheMeng閱讀 730評論 0 0
  • 聊一聊 敏捷管理 每種管理的方式都有邊界 在實際的管理de過程中卻有相通之處 其實敏捷開發(fā)是一種過程控制論,通俗的...
    后龍山大掌柜閱讀 345評論 0 2

友情鏈接更多精彩內(nèi)容