# 操作
Once you have a Moment, you may want to manipulate it in some way. There are a number of methods to help with this.
Moment.js uses the fluent interface pattern, also known as method chaining. This allows you to do crazy things like the following.拥有Moment之后,您可能需要以某种方式对其进行操作。有许多方法可以帮助解决此问题。
Moment.js使用流畅的界面模式,也称为方法链接。这使您可以执行以下疯狂的操作。
moment().add(7, 'days').subtract(1, 'months').year(2009).hours(0).minutes(0).seconds(0);
- Note: It should be noted that moments are mutable. Calling any of the manipulation methods
will change the original moment.
If you want to create a copy and manipulate it, you should usemoment#clonebefore manipulating the moment. More info on cloning. - 注意:应该注意,时刻是可变的。调用任何一种操纵方法“都会改变原始时刻”。如果要创建副本并对其进行操作,则应在操作该时刻之前使用
moment#clone。有关克隆的更多信息。
# 加法
moment().add(Number, String);
moment().add(Duration);
moment().add(Object);
2
3
Mutates the original moment by adding time.
This is a pretty robust function for adding time to an existing moment. To add time, pass the key of what time you want to add, and the amount you want to add.通过增加时间来改变原始moment。
这是一个相当健壮的功能,可以为现有moment增加时间。要增加时间,请输入您想增加时间以及要增加的时间量的键。
moment().add(7, 'days');
There are some shorthand keys as well if you're into that whole brevity thing.
如果您对整个简短内容感兴趣,那么还有一些速记键。
moment().add(7, 'd');
| Key | Shorthand |
|---|---|
| years | y |
| quarters | Q |
| months | M |
| weeks | w |
| days | d |
| hours | h |
| minutes | m |
| seconds | s |
| milliseconds | ms |
If you want to add multiple different keys at the same time, you can pass them in as an object literal.
moment().add(7, 'days').add(1, 'months'); // with chaining
moment().add({days:7,months:1}); // with object literal
2
There are no upper limits for the amounts, so you can overload any of the parameters.
数量没有上限,因此您可以重载任何参数。
moment().add(1000000, 'milliseconds'); // a million milliseconds
moment().add(360, 'days'); // 360 days
2
- Special considerations for months and years
If the day of the month on the original date is greater than the number of days in the final month, the day of the month will change to the last day in the final month.
如果原始日期的月份中的日期大于最后一个月中的天数,则该月中的日期将更改为最后一个月中的最后一天。
moment([2010, 0, 31]); // January 31
moment([2010, 0, 31]).add(1, 'months'); // February 28
2
There are also special considerations to keep in mind when adding time that crosses over daylight saving time. If you are adding years, months, weeks, or days, the original hour will always match the added hour.
在增加跨越夏时制时间的时间时,还需要记住一些特殊的注意事项。如果要添加年,月,周或天,则原始小时将始终与添加的小时匹配。
var m = moment(new Date(2011, 2, 12, 5, 0, 0)); // the day before DST in the US
m.hours(); // 5
m.add(1, 'days').hours(); // 5
2
3
If you are adding hours, minutes, seconds, or milliseconds, the assumption is that you want precision to the hour, and will result in a different hour.
如果要添加小时,分钟,秒或毫秒,则假定您希望精确到小时,并且会导致不同的小时。
var m = moment(new Date(2011, 2, 12, 5, 0, 0)); // the day before DST in the US
m.hours(); // 5
m.add(24, 'hours').hours(); // 6
2
3
- Alternatively, you can use
durationsto add to moments.
var duration = moment.duration({'days' : 1});
moment([2012, 0, 31]).add(duration); // February 1
2
- Before version 2.8.0, the
moment#add(String, Number)syntax was also supported. It has been deprecated in favor ofmoment#add(Number, String).
moment().add('seconds', 1); // Deprecated in 2.8.0
moment().add(1, 'seconds');
2
# 减法
会改变自身的值
const currentTime = moment();
currentTime.subtract(24, 'hours'); //此时currentTime已经变更,不再是当前时间了。如需保持不变需创建其副本,即currentTime.clone()
2
3
moment().subtract(Number, String);
moment().subtract(Duration);
moment().subtract(Object);
2
3
Mutates the original moment by subtracting time.This is exactly the same as moment#add, only instead of adding time, it subtracts time.
moment().subtract(7, 'days');
Before version 2.8.0, the moment#subtract(String, Number)syntax was also supported. It has been deprecated in favor of moment#subtract(Number, String).
moment().subtract('seconds', 1); // Deprecated in 2.8.0
moment().subtract(1, 'seconds');
2
# 开始时间
moment().startOf(String);
Mutates the original moment by setting it to the start of a unit of time.
moment().startOf('year'); // set to January 1st, 12:00 am this year
moment().startOf('month'); // set to the first of this month, 12:00 am
moment().startOf('quarter'); // set to the beginning of the current quarter, 1st day of months, 12:00 am
moment().startOf('week'); // set to the first day of this week, 12:00 am
moment().startOf('isoWeek'); // set to the first day of this week according to ISO 8601, 12:00 am
moment().startOf('day'); // set to 12:00 am today
moment().startOf('hour'); // set to now, but with 0 mins, 0 secs, and 0 ms
moment().startOf('minute'); // set to now, but with 0 seconds and 0 milliseconds
moment().startOf('second'); // same as moment().milliseconds(0);
2
3
4
5
6
7
8
9
These shortcuts are essentially the same as the following.
moment().startOf('year');
moment().month(0).date(1).hours(0).minutes(0).seconds(0).milliseconds(0);
moment().startOf('hour');
moment().minutes(0).seconds(0).milliseconds(0)
2
3
4
- As of version 2.0.0,
moment#startOf('day') replaced moment#sod. - Note:
moment#startOf('week')was added in version 2.0.0. - As of version 2.1.0,
moment#startOf('week')uses the locale aware week start day. - Note:
moment#startOf('isoWeek')was added in version 2.2.0.
# 结束时间
moment().endOf(String);
Mutates the original moment by setting it to the end of a unit of time.This is the same as moment#startOf, only instead of setting to the start of a unit of time, it sets to the end of a unit of time.
moment().endOf("year"); // set the moment to 12-31 11:59:59.999 pm this year
- As of version 2.0.0, moment#endOf('day') replaced moment#eod.
- Note: moment#endOf('week') was added in version 2.0.0.
- As of version 2.1.0, moment#endOf('week') uses the locale aware week start day.
# 操作_最大值
From 2.1.0, Deprecated 2.7.0
moment().max(Moment|String|Number|Date|Array);
- NOTE: This function has been deprecated in 2.7.0. Consider moment.min instead.
Limits the moment to a maximum of another moment value. So a.max(b) is the same as a = moment.min(a, b) (note that max is converted to min).
Sometimes, server clocks are not quite in sync with client clocks. This ends up displaying humanized strings such as "in a few seconds" rather than "a few seconds ago". You can prevent that with moment#max(): - 注意:此功能在2.7.0中已弃用。考虑一下
moment.min。
将力矩限制为另一个力矩值的最大值。因此a.max(b)与a = moment.min(a,b)相同(请注意,max转换为min)。
有时,服务器时钟与客户端时钟不太同步。这样最终显示出人性化的字符串,例如“几秒钟内”而不是“几秒钟前”。您可以使用moment#max()来防止这种情况:
This is the counterpart for moment#min.
这是#min时刻的对应内容。
var momentFromServer = moment(input);
var clampedMoment = momentFromServer.max();
2
You can pass anything to moment#max that you would pass to moment().
moment().max(moment().add(1, 'd'));
moment().max("2013-04-20T20:00:00+0800");
moment().max("Jan 1 2001", "MMM D YYYY");
moment().max(new Date(2012, 1, 8));
2
3
4
# 操作_最小值
From 2.1.0, Deprecated 2.7.0
moment().min(Moment|String|Number|Date|Array);
- NOTE: This function has been deprecated in 2.7.0. Consider moment.max instead.
Limits the moment to a minimum of another moment value. So a.min(b) is the same as a = moment.max(a, b) (note that min is converted to max).
This is the counterpart for moment#max.
moment().min("2013-04-20T20:00:00+0800");
This can be used in conjunction with moment#max to clamp a moment to a range.
var start = moment().startOf('week');
var end = moment().endOf('week');
var actual = moment().min(start).max(end);
2
3
# 操作_本地化
moment().local();
Sets a flag on the original moment to internally use
Date#get*andDate#set*instead ofDate#getUTC* and Date#setUTC*.
var a = moment.utc([2011, 0, 1, 8]);
a.hours(); // 8 UTC
a.local();
a.hours(); // 0 PST
2
3
4
See
moment.utc()for more information on UTC mode.
# 操作_UTC
moment().utc();
Sets a flag on the original moment to internally use
Date#getUTC*andDate#setUTC*instead ofDate#get*andDate#set*.
var a = moment([2011, 0, 1, 8]);
a.hours(); // 8 PST
a.utc();
a.hours(); // 16 UTC
2
3
4
See moment.utc() for more information on UTC mode.
# 操作_UTC偏移量
moment().utcOffset();
moment().utcOffset(Number|String);
2
Get the utc offset in minutes.
- NOTE: Unlike
moment.fn.zonethis function returns the real offset from UTC, not the reverse offset (as returned byDate.prototype.getTimezoneOffset).
Getting the utcOffset of the current object:
moment().utcOffset(); // (-240, -120, -60, 0, 60, 120, 240, etc.)
Setting the utc offset by supplying minutes. Note that once you set an offset, it's fixed and won't change on its own (i.e there are no DST rules). If you want an actual timezone -- time in a particular location, like America/Los_Angeles, consider moment-timezone.
通过提供分钟数来设置utc偏移量。请注意,一旦设置了偏移量,它便会固定不变,并且不会单独更改(即没有DST规则)。如果您想要一个实际的时区-特定位置的时间,例如America /Los_Angeles,请考虑一下“时区”。
moment().utcOffset(120);
If the input is less than 16 and greater than -16, it will interpret your input as hours instead.
如果输入小于16且大于-16,则会将您的输入解释为小时。
// these are equivalent
moment().utcOffset(8); // set hours offset
moment().utcOffset(480); // set minutes offset (8 * 60)
2
3
It is also possible to set the utc offset from a string.
// these are equivalent
moment().utcOffset("+08:00");
moment().utcOffset(8);
moment().utcOffset(480);
2
3
4
moment#utcOffsetwill search the string for the first match of +00:00 +0000 -00:00 -0000, so you can even pass an ISO8601 formatted string and the moment will be changed to that utc offset.
- Note that the string is required to start with the + or - character. Passing a string that does not start with + or - will be interpreted as if it were "+00:00".
请注意,字符串必须以+或-字符开头。传递不以+或-开头的字符串将被解释为好像是“ +00:00”。
moment().utcOffset("2013-03-07T07:00:00+08:00");
# 操作_时区偏移量
From 1.2.0, deprecated 2.9.0+
moment().zone();
moment().zone(Number|String);
2
- NOTE: This function has been deprecated in 2.9.0. Consider moment.fn.utcOffset instead.
Get the timezone offset in minutes.
moment().zone(); // (60, 120, 240, etc.)
- As of version 2.1.0, it is possible to set the offset by passing in the number of minutes offset from GMT.
moment().zone(120);
If the input is less than 16 and greater than -16, it will interpret your input as hours instead.
// these are equivalent
moment().zone(480);
moment().zone(8);
2
3
It is also possible to set the zone from a string.
moment().zone("-08:00");
moment#zonewill search the string for the first match of +00:00 +0000 -00:00 -0000, so you can even pass an ISO8601 formatted string and the moment will be changed to that zone.
moment().zone("2013-03-07T07:00:00-08:00");