# 取值
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.
These map to the corresponding function on the native Date object.Moment.js使用重载的getter和setter方法。您可能会通过在jQuery中使用此模式来熟悉该模式。
在没有参数的情况下调用这些方法将充当获取器,而在具有参数的情况下调用将用作设置器。
这些映射到本机Date对象上的相应函数。
moment().seconds(30) === new Date().setSeconds(30);
moment().seconds() === new Date().getSeconds();
2
If you are in UTC mode, they will map to the UTC equivalent.
如果您处于UTC模式,它们将映射到UTC等效项。
moment.utc().seconds(30) === new Date().setUTCSeconds(30);
moment.utc().seconds() === new Date().getUTCSeconds();
2
For convenience, both singular and plural method names exist as of version 2.0.0.
为方便起见,从版本2.0.0开始,单数和复数方法名称都存在。
- Note: All of these methods mutate the original moment when used as setters.
注意:所有这些方法在用作设置器时都会改变原始力矩。
# 毫秒
moment().millisecond(Number);
moment().millisecond(); // Number
moment().milliseconds(Number);
moment().milliseconds(); // Number
2
3
4
Gets or sets the milliseconds.Accepts numbers from 0 to 999. If the range is exceeded, it will bubble up to the seconds.
获取或设置毫秒。接受0到999之间的数字。如果超出范围,它将冒泡至秒。
# 秒
moment().second(Number);
moment().second(); // Number
moment().seconds(Number);
moment().seconds(); // Number
2
3
4
Gets or sets the seconds.Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the minutes.
获取或设置秒数。接受0到59之间的数字。如果超出该范围,它将冒泡至分钟。
# 分钟
moment().minute(Number);
moment().minute(); // Number
moment().minutes(Number);
moment().minutes(); // Number
2
3
4
Gets or sets the minutes.Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the hours.
# 小时
moment().hour(Number);
moment().hour(); // Number
moment().hours(Number);
moment().hours(); // Number
2
3
4
Gets or sets the hour.Accepts numbers from 0 to 23. If the range is exceeded, it will bubble up to the day.
# 日期
moment().date(Number);
moment().date(); // Number
moment().dates(Number);
moment().dates(); // Number
2
3
4
Gets or sets the day of the month.Accepts numbers from 1 to 31. If the range is exceeded, it will bubble up to the months.
获取或设置一个月中的某一天。接受数字,范围从1到31。如果超出范围,它将冒泡至月份。
- Note: Moment#date is for the date of the month, and Moment#day is for the day of the week.
- 注意:
Moment#date是月份中的日期,而Moment#day是星期中的日期。
- 注意:
# 星期
moment().day(Number|String);
moment().day(); // Number
moment().days(Number|String);
moment().days(); // Number
2
3
4
Gets or sets the day of the week.This method can be used to set the day of the week, with Sunday as 0 and Saturday as 6.If the range is exceeded, it will bubble up to other weeks.
获取或设置星期几。此方法可用于设置星期几,
星期日为0,星期六为6。如果超出该范围,则冒泡其他星期。
moment().day(-7); // last Sunday (0 - 7)
moment().day(7); // next Sunday (0 + 7)
moment().day(10); // next Wednesday (3 + 7)
moment().day(24); // 3 Wednesdays from now (3 + 7 + 7 + 7)
2
3
4
- Note: Moment#date is for the date of the month, and Moment#day is for the day of the week.
As of 2.1.0, a day name is also supported. This is parsed in the moment's current locale.
从2.1.0版开始,还支持日期名称。这是在当前当前语言环境中解析的。
moment().day("Sunday");
moment().day("Monday");
2
# 星期_区域标准
moment().weekday(Number);
moment().weekday(); // Number
2
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(0)将为星期一。如果星期日是一周的第一天,则moment().weekday(0)将是星期日
- As with moment#day, if the range is exceeded, it will bubble up to other weeks.
// 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
2
3
4
5
6
# 星期_ISO
moment().isoWeekday(Number);
moment().isoWeekday(); // Number
2
Gets or sets the ISO day of the week with 1 being Monday and 7 being Sunday.
获取或设置ISO星期几,其中
1为星期一,7为星期日。
moment().isoWeekday(1); // Monday
moment().isoWeekday(7); // Sunday
2
# 天
moment().dayOfYear(Number);
moment().dayOfYear(); // Number
2
Gets or sets the day of the year.Accepts numbers from 1 to 366. If the range is exceeded, it will bubble up to the years.
# 周
moment().week(Number);
moment().week(); // Number
moment().weeks(Number);
moment().weeks(); // Number
2
3
4
Gets or sets the week of the year.Because different locales define week of year numbering differently, Moment.js added moment#week to get/set the localized week of the year.
The week of the year varies depending 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 with January 1st in it is the first week of the year.
In France, Monday is the first day of the week, and the week with January 4th is the first week of the year.
The output of moment#week will depend on the locale for that moment.
When setting the week of the year, the day of the week is retained.获取或设置一年中的星期几,因为不同的语言环境对一年中的星期编号有所不同,因此
Moment.js添加了moment#week来获取/设置一年中的本地化星期。
一年中的一周取决于哪一天是一周中的第一天(星期日,星期一等),以及哪一周是一年中的第一周。
例如,在美国,星期日是一周的第一天。 1月1日所在的一周是一年中的第一周。
在法国,星期一是一周的第一天,而1月4日是一周的第一天。
moment#week的输出将取决于该时刻的语言环境。
设置一年中的星期几时,将保留星期几。
# 周_ISO
moment().isoWeek(Number);
moment().isoWeek(); // Number
moment().isoWeeks(Number);
moment().isoWeeks(); // Number
2
3
4
Gets or sets the ISO week of the year.When setting the week of the year, the day of the week is retained.
获取或设置一年中的ISO星期。设置一年中的星期时,将保留星期几。
# 月
moment().month(Number|String);
moment().month(); // Number
moment().months(Number|String);
moment().months(); // Number
2
3
4
Gets or sets the month.Accepts numbers from
0 to 11. If the range is exceeded, it will bubble up to the year.
- Note: Months are zero indexed, so January is month 0.
- As of 2.1.0, a month name is also supported. This is parsed in the moment's current locale.
从2.1.0版开始,还支持月份名称。这是在当前当前语言环境中解析的。
moment().month("January");
moment().month("Feb");
2
Before version 2.1.0, if a moment changed months and the new month did not have enough days to keep the current day of month, it would overflow to the next month.
As of version 2.1.0, this was changed to be clamped to the end of the target month.在版本2.1.0之前,如果片刻更改了几个月,而新月份没有足够的天数来保留当月的当前日期,则它将溢出到下个月。
从版本2.1.0开始,已将其更改为限制在目标月末。
// before 2.1.0
moment([2012, 0, 31]).month(1).format("YYYY-MM-DD"); // 2012-03-02
// after 2.1.0
moment([2012, 0, 31]).month(1).format("YYYY-MM-DD"); // 2012-02-29
2
3
4
# 季度
moment().quarter(); // Number
moment().quarter(Number);
2
Gets the quarter (1 to 4).
moment('2013-01-01T00:00:00.000').quarter() // 1
moment('2013-04-01T00:00:00.000').subtract(1, 'ms').quarter() // 1
moment('2013-04-01T00:00:00.000').quarter() // 2
moment('2013-07-01T00:00:00.000').subtract(1, 'ms').quarter() // 2
moment('2013-07-01T00:00:00.000').quarter() // 3
moment('2013-10-01T00:00:00.000').subtract(1, 'ms').quarter() // 3
moment('2013-10-01T00:00:00.000').quarter() // 4
moment('2014-01-01T00:00:00.000').subtract(1, 'ms').quarter() // 4
2
3
4
5
6
7
8
Sets the quarter (1 to 4).
moment('2013-01-01T00:00:00.000').quarter(2) // '2013-04-01T00:00:00.000'
moment('2013-02-05T05:06:07.000').quarter(2).format() // '2013-05-05T05:06:07-07:00'
2
# 年
moment().year(Number);
moment().year(); // Number
moment().years(Number);
moment().years(); // Number
2
3
4
Gets or sets the year.Accepts numbers from
-270,000 to 270,000.
# 周年_区域标准
moment().weekYear(Number);
moment().weekYear(); // Number
2
Gets or sets the week-year according to the locale.Because the first day of the first week does not always fall on the first day of the year, sometimes the week-year will differ from the month year.
根据区域设置获取或设置周年。由于第一周的第一天并不总是在一年的第一天,因此有时周年与月份年会有所不同。
For example, in the US, the week that contains Jan 1 is always the first week. In the US, weeks also start on Sunday. If Jan 1 was a Monday, Dec 31 would belong to the same week as Jan 1, and thus the same week-year as Jan 1. Dec 30 would have a different week-year than Dec 31.
例如,在美国,包含1月1日的一周始终是第一周。在美国,星期几也从星期日开始。如果1月1日为星期一,则12月31日与1月1日属于同一周,因此与1月1日相同的周年。12月30日与12月31日将具有不同的周年。
# 周年_ISO
moment().isoWeekYear(Number);
moment().isoWeekYear(); // Number
2
Gets or sets the ISO week-year.
# 周数
moment().weeksInYear();
Gets the number of weeks according to locale in the current moment's year.
# 周数_ISO
moment().isoWeeksInYear();
Gets the number of weeks in the current moment's year, according to ISO weeks.
# 取值_取值
moment().get('year');
moment().get('month'); // 0 to 11
moment().get('date');
moment().get('hour');
moment().get('minute');
moment().get('second');
moment().get('millisecond');
2
3
4
5
6
7
String getter. In general
moment().get(unit) === moment()[unit]()
Units are case insensitive, and support plural and short forms: year (years, y), month (months, M), date (dates, D), hour (hours, h), minute (minutes, m), second (seconds, s), millisecond (milliseconds, ms).
# 取值_赋值
moment().set(String, Int);
moment().set(Object(String, Int));
2
Generic setter, accepting unit as first argument, and value as second:
通用设置器,接受单位为第一个参数,值为第二个:
moment().set('year', 2013);
moment().set('month', 3); // April
moment().set('date', 1);
moment().set('hour', 13);
moment().set('minute', 20);
moment().set('second', 30);
moment().set('millisecond', 123);
moment().set({'year': 2013, 'month': 3});
2
3
4
5
6
7
8
9
Units are case insensitive, and support plural and short forms: year (years, y), month (months, M), date (dates, D), hour (hours, h), minute (minutes, m), second (seconds, s), millisecond (milliseconds, ms).
- Object parsing was added in 2.9.0
# 最大值
moment.max(Moment[,Moment...]);
Returns the maximum (most distant future) of the given moment instances.
For example:
var a = moment().subtract(1, 'day');
var b = moment().add(1, 'day');
moment.max(a, b); // b
2
3
With no arguments the function returns a moment instance with the current time.
不带参数的函数将返回带有当前时间。
- From version 2.10.5, if an invalid moment is one of the arguments, the result is an invalid moment.
moment.max(moment(), moment.invalid()).isValid() === false
moment.max(moment.invalid(), moment()).isValid() === false
2
# 最小值
moment.min(Moment[,Moment...]);
Returns the minimum (most distant past) of the given moment instances.
For example:
var a = moment().subtract(1, 'day');
var b = moment().add(1, 'day');
moment.min(a, b); // a
2
3
With no arguments the function returns a moment instance with the current time.
不带参数的函数将返回带有当前时间
- From version 2.10.5, if an invalid moment is one of the arguments, the result is an invalid moment.
moment.min(moment(), moment.invalid()).isValid() === false
moment.min(moment.invalid(), moment()).isValid() === false
2