Use of moment.js
1. To get the current date and time, just call moment() without parameters.
var now = moment();
This is basically the same as calling moment(new Date()).
2. Time can be created by passing in a string
var day = moment("1995-12-25");
Different browsers parse strings in different formats, so changing to other browsers may result in different formats.
If you know the format of the input string, you can use it to parse the moment.
moment("12-25-1995", "MM-DD-YYYY");
Tokens for year, month, date Tokens
are case sensitive.
enter | example | describe |
---|---|---|
YYYY | 2014 | 4 or 2 digit year |
YY | 14 | 2 digit year |
YY | 14 | 2 digit year |
Y | -25 | Year with arbitrary numbers and symbols |
Q | 1…4 | The quarter of the year. Set month to first month of quarter |
M | MM | 1…December numbers |
MMM MMMM | Jan...December | The month name in the locale, represented by |
D DD | 1…31 | day of the month |
Do | 1st…31st | day of the month, with ordinal number |
DDDD DDDD | 1…365 | day of the year |
X | 1410715640.579 | Unix timestamp |
x | 1410715640579 | Unix millisecond timestamp |
3、day()
moment().day(Number|String);
moment().day(); // 数字
moment().days(Number|String);
moment().days(); // 数字
Get or set the day of the week.
This method can be used to set the day of the week, where Sunday is 0 and Saturday is 6.
If the given value is 0 to 6, the date of the result will be the current (Sunday to Saturday) week.
If it's out of range, it will bubble up to other weeks.
moment().day(-7); // 上个星期日 (0 - 7)
moment().day(0); // 这个星期日 (0)
moment().day(7); // 下个星期日 (0 + 7)
moment().day(10); // 下个星期三 (3 + 7)
moment().day(24); // 从现在起第 3 个星期三 (3 + 7 + 7 + 7)
moment().day() is to get today's time
4、week()
moment().week(Number);
moment().week(); // 数字
moment().weeks(Number);
moment().weeks(); // 数字
moment().week(); In fact, it is to get the current week is the first week of this year.
moment().week(Number) with parameters is to get the first week of this year.
Gets or sets the week of the year.
Since different locales define the week of the year differently, Moment.js adds moment#week to get/set the localized week of the year.
The week of the year depends on which day is the first day of the week (Sunday, Monday, etc.), and which week is the first week of the year.
For example, in the United States, Sunday is the first day of the week. The week of January 1 is the first week of the year.
In France, Monday is the first day of the week and January 4th is the first week of the year.
5、month()
moment().month(Number|String);
moment().month(); // 数字
moment().months(Number|String);
moment().months(); // 数字
moment().month() is to get the current month of this year
moment().month (parameter) is to get the current parameter is the month of this year
Number is the month
String is the name of the supported month, such as
moment().month("January");
Get the Monday to Sunday of the last i week:
const startDate = moment().week(moment().week() - i).startOf('week').add(1,'days').valueOf();
const endDate = moment().week(moment().week() - i).endOf('week').add(1,'days').valueOf();
Get the first and last day of the previous month:
const startDate = moment().month(moment().month() - 1).startOf('month').valueOf();
const endDate = moment().month(moment().month() - 1).endOf('month').valueOf();
Get the first and last day of the previous 3 months:
const startDate = moment().month(moment().month() - 3).startOf('month').valueOf();
const endDate = moment().month(moment().month() - 1).endOf('month').valueOf();
Get the first and last day of the previous i months:
const startDate = moment().month(moment().month() - i).startOf('month').valueOf();
const endDate = moment().month(moment().month() - 1).endOf('month').valueOf();