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