使用moment.js轻松管理日期和时间
2017-09-07 10:39
721 查看
moment.js不依赖任何第三方库,支持字符串、Date、时间戳以及数组等格式,可以像PHP的date()函数一样,格式化日期时间,计算相对时间,获取特定时间后的日期时间等等,本文有如下举例。
当前时间:
今天是星期几:
转换当前时间的Unix时间戳:
20120901相对当前日期是2年前
7天后的日期:
9小时后的时间:
当前时间:moment().format('YYYY-MM-DD HH:mm:ss');
2017-10-25 10:21:40
今天是星期几:moment().format('d');
3
Unix时间戳:moment().format('X');1508898100
相对时间:moment("20130101", "YYYYMMDD").fromNow();
5 years ago
相对时间:moment("20151010", "YYYYMMDD").fromNow();
2 years ago
7天后的日期:moment().add('days',7).format('YYYY年MM月DD日');
2017年11月01日
9小时后:moment().add('hours',9).format('HH:mm:ss');19:21:40
明天moment().add(1, 'days').calendar();
Tomorrow at 10:21 AM
moment.js提供了丰富的说明文档,使用它还可以创建日历项目等复杂的日期时间应用。我们日常开发中最常用的是格式化时间,下面我把常用的格式制作成表格说明供有需要的朋友查看:
获取当前时间,方法等同于 new Date()
字符串 1.0.0+
When creating a moment from a string, we first check if the string matches known ISO 8601 formats,
then fall back to
Warning: Browser support for parsing strings is inconsistent. Because there
is no specification on which formats should be supported, what works in some browsers will not work in other browsers.
For consistent results parsing anything other than ISO 8601 strings, you should use String
+ Format.
An ISO 8601 string requires a date part.
A time part can also be included, separated from the date part by a space or a uppercase T.
Any of the date parts can have a time part.
If a time part is included, an offset from UTC can also be included as
or
Note: Automatic cross browser ISO-8601 support was added in version 1.5.0. Support for the week and ordinal formats was added in version 2.3.0.
If a string does not match any of the above formats and is not able to be parsed with
return false.
编辑
If you know the format of an input string, you can use that to parse a moment.
The parser ignores non-alphanumeric characters, so both of the following will return the same thing.
The parsing tokens are similar to the formatting tokens used in
2 digit years, and converts them to a year near 2000 (same as
For these, the lowercase tokens use the locale aware week start days, and the uppercase tokens use the ISO
week date start days.
From version 2.10.5: fractional second tokens length 4 up to 9 can parse any number of digits, but will only consider the top 3 (milliseconds). Use if you have the time printed with many fractional
digits and want to consume the input.
Locale aware date and time formats are also available using
except
Unless you specify a timezone offset, parsing a string will create a date in the current timezone.
If the moment that results from the parsed input does not exist,
As of version
Moment's parser is very forgiving, and this can lead to undesired behavior. As of version
for the last argument to make Moment use strict parsing. Strict parsing requires that the format and input match exactly.
You can use both locale and strictness.
By default, two digit years above 68 are assumed to be in the 1900's and years 68 or below are assumed to be in the 2000's. This can be changed by replacing the
编辑
If you don't know the exact format of an input string, but know it could be one of many, you can use an array of formats.
This is the same as String + Format, only it will try to match the input to multiple formats.
Starting in version
Prefer formats resulting in valid dates over invalid ones.
Prefer formats that parse more of the string than less and use more of the format than less, i.e. prefer stricter parsing.
Prefer formats earlier in the array than later.
You may also specify a locale and strictness argument. They work the same was as they do in the single format case.
Note: Parsing multiple formats is considerably slower than parsing a single format. If you can avoid it, it is much faster to parse a single format.
编辑
ISO-8601 is a standard for time and duration display. Moment already supports parsing iso-8601 strings,
but this can be specified explicitly in the format/list of formats when constructing a moment.
To specify iso-8601 parsing use
Examples:
编辑
You can create a moment by specifying some of the units in an object.
Omitted units default to 0 or the current date, month, and year.
day-of-the-month.
Note that like
编辑
Similar to
the Unix Epoch (Jan 1 1970 12AM UTC).
编辑
To create a moment from a Unix timestamp (seconds since the Unix Epoch), use
This is implemented as
编辑
You can create a
This clones
affect the
编辑
You can create a moment with an array of numbers that mirror the parameters passed to new
Date()
Any value past the year is optional, and will default to the lowest possible number.
Construction with an array will create a date in the current timezone. To create a date from an array at UTC, use
Note: Because this mirrors the native
are all zero indexed. Years and days of the month are 1 indexed.
This is often the cause of frustration, especially with months, so take note!
If the date represented by the array does not exist,
编辑
Microsoft Web API returns JSON dates in proper ISO-8601 format by default, but older ASP.NET technologies may return dates in JSON as
If a string that matches this format is passed in, it will be parsed correctly.
编辑
All moments are mutable. If you want a clone of a moment, you can do so explicitly or implicitly.
Calling
Additionally, you can call
编辑
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use
This brings us to an interesting feature of Moment.js. UTC mode.
While in UTC mode, all display methods will display in UTC time instead of local time.
Additionally, while in UTC mode, all getters and setters will internally use the
instead of the
It is important to note that though the displays differ above, they are both the same moment in time.
Any moment created with
not.
To switch from UTC to local time, you can use moment#utc or moment#local.
编辑
Moment normally interprets input times as local times (or UTC times if
string itself contains time zone information.
parse the zone.
Note: this method only works for a single string argument, not a string and format.
编辑
Moment applies stricter initialization rules than the
You can check whether the Moment considers the date invalid using
which returns an object.
The following parsing flags result in an invalid date:
etc.
below);
Contains the invalid month string itself, or else null.
Boolean.
Boolean.
Additionally, if the Moment is parsed in strict mode, these flags must be empty for the Moment to be valid:
Note: Moment's concept of validity became more strict and consistent between 2.2 and 2.3.
Additionally, you can use
The return value has the following meaning:
years
months
days
hours
minutes
seconds
milliseconds
Note: In case of multiple wrong units the first one is returned (because days validity may depend on month, for example).
编辑
You can create a moment object specifying only some of the units, and the rest will be defaulted to the current day, month or year, or 0 for hours, minutes, seconds and milliseconds.
Defaulting to now, when nothing is passed:
Defaulting to today, when only hours, minutes, seconds and milliseconds are passed:
Defaulting to this month and year, when only days and smaller units are passed:
Defaulting to this year, if year is not specified:
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
If you are in UTC mode, they will map to the UTC equivalent.
For convenience, both singular and plural method names exist as of version
Note: All of these methods mutate the original moment when used as setters.
编辑
Gets or sets the milliseconds.
Accepts numbers from 0 to 999. If the range is exceeded, it will bubble up to the seconds.
编辑
Gets or sets the seconds.
Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the minutes.
编辑
Gets or sets the minutes.
Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the hours.
编辑
Gets or sets the hour.
Accepts numbers from 0 to 23. If the range is exceeded, it will bubble up to the day.
编辑
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.
Note:
for the day of the week.
编辑
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.
Note:
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.
编辑
Gets or sets the day of the week according to the locale.
If the locale assigns Monday as the first day of the week,
of the week,
As with
编辑
Gets or sets the ISO day of the week with
Monday and
编辑
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.
编辑
Gets or sets the week of the year.
Because different locales define week of year numbering differently, Moment.js added
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
that moment.
When setting the week of the year, the day of the week is retained.
编辑
Gets or sets the ISO week of the year.
When setting the week of the year, the day of the week is retained.
编辑
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.
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.
编辑
Gets the quarter (1 to 4).
Sets the quarter (1 to 4).
编辑
Gets or sets the year.
Accepts numbers from -270,000 to 270,000.
编辑
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.
编辑
Gets or sets the ISO week-year.
编辑
Gets the number of weeks according to locale in the current moment's year.
编辑
Gets the number of weeks in the current moment's year, according to ISO weeks.
编辑
String getter. In general
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).
编辑
Generic setter, accepting unit as first argument, and value as second:
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
编辑
Returns the maximum (most distant future) of the given moment instances.
For example:
With no arguments the function returns a moment instance with the current time.
From version
编辑
Returns the minimum (most distant past) of the given moment instances.
For example:
With no arguments the function returns a moment instance with the current time.
From version
Once you have a
Moment.js uses the fluent interface pattern, also known as method
chaining. This allows you to do crazy things like the following.
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 use
info on cloning.
编辑
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.
There are some shorthand keys as well if you're into that whole brevity thing.
If you want to add multiple different keys at the same time, you can pass them in as an object literal.
There are no upper limits for the amounts, so you can overload any of the parameters.
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.
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.
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.
Alternatively, you can use durations to add to moments.
Before version
编辑
Mutates the original moment by subtracting time.
This is exactly the same as
Before version
编辑
Mutates the original moment by setting it to the start of a unit of time.
These shortcuts are essentially the same as the following.
As of version 2.0.0,
Note:
As of version 2.1.0,
Note:
编辑
Mutates the original moment by setting it to the end of a unit of time.
This is the same as
of time.
As of version 2.0.0,
Note:
As of version 2.1.0,
编辑
NOTE: This function has been deprecated in 2.7.0. Consider
Limits the moment to a maximum of another moment value. So
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
This is the counterpart for
You can pass anything to
编辑
NOTE: This function has been deprecated in 2.7.0. Consider
Limits the moment to a minimum of another moment value. So
This is the counterpart for
This can be used in conjunction with
编辑
Sets a flag on the original moment to internally use
of
See moment.utc() for more information on UTC mode.
编辑
Sets a flag on the original moment to internally use
of
See moment.utc() for more information on UTC mode.
编辑
Get the utc offset in minutes.
NOTE: Unlike
function returns the real offset from UTC, not the reverse offset (as returned by
Getting the utcOffset of the current object:
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
If the input is less than
it will interpret your input as hours instead.
It is also possible to set the utc offset from a string.
Note that the string is required to start with the
Passing a string that does not start with
if it were
编辑
NOTE: This function has been deprecated in 2.9.0. Consider
Get the timezone offset in minutes.
As of version 2.1.0, it is possible to set the offset by passing in the number of minutes offset from GMT.
If the input is less than
it will interpret your input as hours instead.
It is also possible to set the zone from a string.
Once parsing and manipulation are done, you need some way to display the moment.
编辑
This is the most robust display option. It takes a string of tokens and replaces them with their corresponding values.
in 2.10.5. They display 3 significant digits and the rest is filled with zeros.
Because preferred formatting differs based on locale, there are a few tokens that can be used to format a moment based on its locale.
There are upper and lower case variations on the same formats. The lowercase version is intended to be the shortened version of its uppercase counterpart.
To escape characters in format strings, you can wrap the characters in square brackets.
Note: While these date formats are very similar to LDML date formats, there are a few minor differences regarding day of month, day of year, and day of week.
For a breakdown of a few different date formatting tokens across different locales, see this
chart of date formatting tokens.
To compare Moment.js formatting speed against other libraries, check out this comparison against other libraries.
If you are more comfortable working with strftime instead of LDML-like parsing tokens, you can use Ben Oakes' plugin. benjaminoakes/moment-strftime.
As of version 1.5.0, calling
Out of the box,
编辑
A common way of displaying time is handled by
If you pass
The base strings are customized by the current locale.
The breakdown of which string is displayed for each length of time is outlined in the table below.
From version 2.10.3, if the target moment object is invalid the result is the localized Invalid date string.
编辑
You may want to display a moment in relation to a time other than now. In that case, you can use
The first parameter is anything you can pass to
Like
the second parameter returns value without the suffix. This is useful wherever you need to have a human readable length of time.
From version 2.10.3, if any of the endpoints are invalid the result is the localized Invalid date string.
编辑
A common way of displaying time is handled by
This is similar to
but gives the opposite interval:
This is similar to
but is special-cased for the current time. Use
If you pass
The base strings are customized by the current locale.
The breakdown of which string is displayed for each length of time is outlined in the table below.
From version 2.10.3, if the target moment object is invalid the result is the localized Invalid date string.
编辑
You may want to display a moment in relation to a time other than now. In that case, you can use
The first parameter is anything you can pass to
Like
the second parameter returns value without the suffix. This is useful wherever you need to have a human readable length of time.
From version 2.10.3, if any of the endpoints are invalid the result is the localized Invalid date string.
编辑
Calendar time displays time relative to a given
date (today by default) the date is.
These strings are localized, and can be customized.
From 2.10.5 moment supports specifying calendar output formats per invocation:
编辑
To get the difference in milliseconds, use
To get the difference in another unit of measurement, pass that measurement as the second argument.
The supported measurements are years, months, weeks, days, hours, minutes, and seconds. For ease of development, the singular forms are supported as of 2.0.0. Units of measurement other than
milliseconds are available in version 1.1.1.
By default,
the third argument. Before 2.0.0,
If the moment is earlier than the moment you are passing to
A easy way to think of this is by replacing
date are always a whole number apart.
So Jan 15 to Feb 15 should be exactly 1 month.
Feb 28 to Mar 28 should be exactly 1 month.
Feb 28 2011 to Feb 28 2012 should be exactly 1 year.
See more discussion on the month and year diffs here
This change to month and year diffs was made in 2.0.0. As of version 2.9.0 diff also support quarter unit.
编辑
To get a Unix timestamp (the number of seconds since the epoch) from a
编辑
This value is floored to the nearest second, and does not include a milliseconds component.
编辑
Get the number of days in the current month.
编辑
To get the native Date object that Moment.js wraps, use
This will return the
cause the moment to change. If you want a
you use
has been deprecated as of 1.6.0.
编辑
This returns an array that mirrors the parameters from
编辑
When serializing an object to JSON, if there is a
to UTC.
If instead you would like an ISO8601 string that reflects the moment's
like this:
This changes the behavior as follows:
编辑
Formats a string to the ISO8601 standard.
From version 2.8.4 the native
编辑
This returns an object containing year, month, day-of-month, hour, minute, seconds, milliseconds.
编辑
Check if a moment is before another moment.
If you want to limit the granularity to a unit other than milliseconds, pass the units as the second parameter.
As the second parameter determines the precision, and not just a single value to check, using day will check for year, month and day.
Like
any of the units of time that are supported for
If nothing is passed to
NOTE:
initial created moment would be the same as the one created in isBefore to perform the check, so the result would be
that the moment created in isBefore is measurably after the one created in
编辑
Check if a moment is the same as another moment.
If you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter.
When including a second parameter, it will match all units equal or larger. Passing in
Passing in
and
Like
any of the units of time that are supported for
编辑
Check if a moment is after another moment.
If you want to limit the granularity to a unit other than milliseconds, pass the units as the second parameter.
As the second parameter determines the precision, and not just a single value to check, using day will check for year, month and day.
Like
any of the units of time that are supported for
If nothing is passed to
编辑
Check if a moment is between two other moments, optionally looking at unit scale (minutes, hours, days, etc). The match is exclusive.
If you want to limit the granularity to a unit other than milliseconds, pass the units as the second parameter.
Like
of the units of time that are supported for
Year, month, week, day, hour, minute, and second.
编辑
that year is a leap year, and
编辑
编辑
Another important piece of validation is to know if the date has been moved by a DST. For example, in most of the United States:
This is because daylight saving time shifts the time from 2:00 to 3:00, so 2:30 isn't a real time. The resulting time is browser-dependent, either adjusting the time forward or backwards. Use
test for this condition.
Note: before 2.3.0, Moment objects in this condition always returned
they now return
编辑
To check if a variable is a moment object, use
编辑
To check if a variable is a native js Date object, use
Moment.js has robust support for internationalization.
You can load multiple locales and easily switch between them.
In addition to assigning a global locale, you can assign a locale to a specific moment.
编辑
By default, Moment.js comes with English locale strings. If you need other locales, you can load them into Moment.js for later use.
To load a locale, pass the key and the string values to
More details on each of the parts of the locale bundle can be found in the customization section.
Once you load a locale, it becomes the active locale. To change active locales, simply call
a loaded locale.
As of 2.8.0, changing the global locale doesn't affect existing instances.
specify.
You may also specify a list of locales, and Moment will use the first one it has localizations for.
Moment will also try locale specifier substrings from most-specific to least-specific until it finds a locale it knows. This is useful when supplying Moment with a locale string pulled from the user's environment,
such as
Finally, Moment will search intelligently through an array of locales and their substrings.
编辑
A global locale configuration can be problematic when passing around moments that may need to be formatted into different locale.
In 1.7.0 we added instance specific locale configurations.
If you call
If you need to access the locale data for a moment, this is the preferred way to do so.
As of 2.3.0, you can also specify an array of locale identifiers. It works the same was it does in the global
locale configuration.
编辑
Loading locales in NodeJS is super easy. If there is a locale file in
call to
If you want your locale supported, create a pull request to the
locale and unit test files.
编辑
Loading locales in the browser just requires you to include the locale files.
There are minified versions of all locales together:
To minimize http requests, use our Grunt task to compile Moment with a custom list of locales:
Note: Locale files are defined in UMD style, so they should work seamlessly in all environments.
编辑
To add your locale to Moment.js, submit a pull request with both a locale file and a test file. You can find examples in
To run the tests in Node.js, do
If all the tests pass, submit a pull request, and thank you for contributing!
编辑
If you are changing locales frequently, you may want to know what locale is currently being used. This is as simple as calling
any parameters.
编辑
It is sometimes useful to get the list of months or weekdays in a locale, for example when populating a dropdown menu.
Returns the list of months in the current locale.
Similarly,
lists of weekdays.
You can pass an integer into each of those functions to get a specific month or weekday.
Note: Currently, weekdays always have Sunday as index 0, regardless of the local first day of the week.
Some locales make special considerations into account when formatting month names. For example, Dutch formats month abbreviations without a trailing period, but only if it's formatting the month between dashes.
The
And finally, you can combine both the format option and the integer option.
编辑
You can access the properties of the currently loaded locale through the
current locale or a locale with the given key:
The returned object has the following methods:
Moment.js is very easy to customize. In general, you should create a locale setting with your customizations.
However, you can also overwrite an existing locale that has been loaded as well.
Any settings that are not defined are inherited from the default english settings.
You can remove a previously defined locale by passing
available for use.
编辑
If you need more processing to calculate the name of the month, (for example, if there is different grammar for different formats),
be a function with the following signature. It should always return a month name.
编辑
Like
be a callback function as well.
编辑
编辑
编辑
pickers, thus they should be as small as possible.
编辑
You can eliminate the lowercase
token variants.
编辑
to the prefix/suffix for past dates. For all others, a single character refers to the singular, and a double character refers to the plural.
If a locale requires additional processing for a token, it can set the token as a function with the following signature. The function should return a string.
The
(eg.
The
the number is the number of minutes, etc.
The
with a suffix. (The reason for the inverted logic is because the default behavior is to display with the suffix.)
The
prefix/suffix. The
编辑
If your locale uses 'am/pm',
If your locale needs any different computation for am/pm,
correct string based on hour, minute, and upper/lowercase.
Before version 1.6.0,
This has been deprecated. The 1.6.0 callback function syntax is now used instead.
编辑
To configure what strings should be parsed as input, set the
编辑
Each of the
a formatting string.
编辑
As of 2.0.0, the ordinal function should return both the number and the ordinal. Previously, only the ordinal was returned.
As of 2.1.0, the token parameter was added. It is a string of the token that is being ordinalized, for example:
For more information on ordinal numbers, see wikipedia
编辑
more than 45 seconds is considered a minute, more than 22 hours is considered a day and so on. To change those cutoffs use
limit is one of
NOTE: Retrieving thresholds was added in 2.8.1.
Moment.js also has duration objects. Where a moment is defined as single points in time, durations are defined as a length of time.
Durations do not have a defined beginning and end date. They are contextless.
A duration is conceptually more similar to '2 hours' than to 'between 2 and 4 pm today'. As such, they are not a good solution to converting between units that depend on context.
For example, a year can be defined as 366 days, 365 days, 365.25 days, 12 months, or 52 weeks. Trying to convert years to days makes no sense without context. It is much better to use
calculating days or years between two moments than to use
编辑
To create a duration, call
If you want to create a moment with a unit of measurement other than milliseconds, you can pass the unit of measurement as well.
The same shorthand for
here as well.
Much like
As of 2.1.0, moment supports parsing ASP.NET style time spans. The following formats are supported.
The format is an hour, minute, second string separated by colons like
a dot separator like so
As of 2.3.0, moment also supports parsing ISO 8601 durations.
编辑
Sometimes, you want all the goodness of
display a length of time.
Enter
By default, the return string is suffixless. If you want a suffix, pass in true as seen below.
For suffixes before now, pass in a negative number.
编辑
To get the number of milliseconds in a duration, use
It will return a number between 0 and 999.
If you want the length of the duration in milliseconds, use
编辑
To get the number of seconds in a duration, use
It will return a number between 0 and 59.
If you want the length of the duration in seconds, use
编辑
As with the other getters for durations,
编辑
As with the other getters for durations,
编辑
As with the other getters for durations,
编辑
As with the other getters for durations,
Note: The length of a duration in months is defined as 30 days.
编辑
As with the other getters for durations,
Note: The length of a duration in years is defined as 365 days.
编辑
Mutates the original duration by adding time.
The same keys and shorthands used to create durations can be used here as the second argument.
编辑
Mutates the original duration by subtracting time.
The same keys and shorthands used to create durations can be used here as the second argument.
编辑
As an alternate to
All the shorthand keys from
as well.
编辑
As an alternate to
All the shorthand keys from
as well.
编辑
When serializing a duration object to JSON, it will be be represented as an ISO8601 string.
编辑
To check if a variable is a moment duration object, use
Moment exposes some methods which may be useful to people extending the library or writing custom parsers.
编辑
Many of Moment's functions allow the caller to pass in aliases for unit enums. For example, all of the
If you're extending the library, you may want access to Moment's facilities for that in order to better align your functionality with Moment's.
编辑
You can create your own invalid Moment objects, which is useful in making your own parser.
flag unless it's one of the properties specified.
You need not specify parsing flags recognized by Moment; the Moment will be invalid nonetheless, and the parsing flags will be returned by
Some other people have made plugins for Moment.js that may be useful to you.
编辑
If you are more comfortable working with strftime instead of LDML-like parsing tokens, you can use Ben Oakes' plugin
The repository is located at github.com/benjaminoakes/moment-strftime
编辑
If you are looking for a Python-like isocalendar method, you can use Rocky Meza's plugin
Calling the isocalendar method on a moment will return an array like the following:
You can also reconstruct a moment from a isocalendar array.
The repository is located at github.com/fusionbox/moment-isocalendar
编辑
If you need to work with date ranges, you can use Gianni Chiappetta's plugin
Documentation can be found on the homepage github.com/gf3/moment-range.
And it is also available for the web at the repository below.
The repository is located at github.com/gf3/moment-range
编辑
Another range plugin is Isaac Cambron's library
readably. For example,
Full documentation of all the options and features is here.
It's available on npm like so:
Or just grab the JS file from here.
编辑
If you're trying to format times for tweets like the way Twitter does, you can use the moment.twitter plugin
by @hijonathan.
It's a simple way to display both short and long versions of human-readable timestamps.
Yes, it does smart pluralization.
Not short enough for you?
编辑
If you want to work with Jalaali calendar system (Jalali, Persian, Khorshidi or Shamsi), you can use Behrang Noruzi Niya's plugin
When installed, it will wrap
example:
The repository is located at github.com/behrang/moment-jalaali.
编辑
If you are using OLE Automation dates in .NET check out Markit On Demand's
to format OA dates into JavaScript dates and vice-versa.
Convert a
Or, convert an OA date to a
More information and detailed docs can be found on GitHub at http://markitondemand.github.io/moment-msdate/.
编辑
If you ever have need for Fiscal, Calendar or Academic quarters, you can use the moment-fquarter plugin
by @robgallen.
At its simplest, just call the fquarter method on any moment object. It returns a formatted string with April being the first quarter.
You can pass in any month as the starting quarter, e.g. July
If you want calendar quarters, start in January
编辑
The Precise Range plugin, written by Rob
Dawson, can be used to display exact, human-readable representations of date/time ranges
编辑
If you need to work with recurring dates, you can use Casey Trimm's plugin
This plugin will allow you to create length-based intervals (days, weeks, etc.) and calendar-based intervals (daysOfMonth, monthsOfYear, etc.).
It provides a
to get the next and previous dates in a series.
The repository, documentation, and many more examples can be found at github.com/c-trimm/moment-recur
编辑
This plugin extracts the format of a date/time string.
That allows to create smart date inputs that let your users set a Date/Time and lets you extract the user's preferred format for future usage. Find an example usage of it at minutes.io.
The Plugin has been authored by @gr2m. Links: Demo | Source
编辑
If you want to work with the
For example,
The repository is located at github.com/MadMG/moment-jdateformatparser
编辑
If you want to work with Hijri calendar then you can use
a moment plugin for the Hijri lunar calendar based on Umm al-Qura calculations. This plugin is developed by Suhail
Alkowaileet.
When you install it, it will wrap
The repository is located at github.com/xsoh/moment-hijri.
编辑
a plugin that manipulated dates through patterns. You can use basic operations –set/add/substract– on individual parts (hours, month, …) of a Moment instance.
Optional parameters lets you specify custom patterns and force strict pattern usage (non-alphabetic characters are not mandatory in passed string by default).
更多有关moment.js的介绍,请访问项目官网:http://momentjs.com/
当前时间:moment().format('YYYY-MM-DD HH:mm:ss');
2017-09-07 10:36:14
今天是星期几:moment().format('d');
4
Unix时间戳:moment().format('X');1504751774
相对时间:moment("20130101", "YYYYMMDD").fromNow();
5 years ago
相对时间:moment("20151010", "YYYYMMDD").fromNow();
2 years ago
7天后的日期:moment().add('days',7).format('YYYY年MM月DD日');
2017年09月14日
9小时后:moment().add('hours',9).format('HH:mm:ss');19:36:14
明天moment().add(1, 'days').calendar();
Tomorrow at 10:36 AM
格式化日期
当前时间:moment().format('YYYY-MM-DD HH:mm:ss'); //2014-09-24 23:36:09
今天是星期几:
moment().format('d'); //3
转换当前时间的Unix时间戳:
moment().format('X');
相对时间
20120901相对当前日期是2年前moment("20120901", "YYYYMMDD").fromNow(); //2 years ago
7天后的日期:
moment().add('days',7).format('YYYY年MM月DD日'); //2014年10月01日
9小时后的时间:
moment().add('hours',9).format('HH:mm:ss');
当前时间:moment().format('YYYY-MM-DD HH:mm:ss');
2017-10-25 10:21:40
今天是星期几:moment().format('d');
3
Unix时间戳:moment().format('X');1508898100
相对时间:moment("20130101", "YYYYMMDD").fromNow();
5 years ago
相对时间:moment("20151010", "YYYYMMDD").fromNow();
2 years ago
7天后的日期:moment().add('days',7).format('YYYY年MM月DD日');
2017年11月01日
9小时后:moment().add('hours',9).format('HH:mm:ss');19:21:40
明天moment().add(1, 'days').calendar();
Tomorrow at 10:21 AM
moment.js提供了丰富的说明文档,使用它还可以创建日历项目等复杂的日期时间应用。我们日常开发中最常用的是格式化时间,下面我把常用的格式制作成表格说明供有需要的朋友查看:
格式代码 | 说明 | 返回值例子 |
M | 数字表示的月份,没有前导零 | 1到12 |
MM | 数字表示的月份,有前导零 | 01到12 |
MMM | 三个字母缩写表示的月份 | Jan到Dec |
MMMM | 月份,完整的文本格式 | January到December |
Q | 季度 | 1到4 |
D | 月份中的第几天,没有前导零 | 1到31 |
DD | 月份中的第几天,有前导零 | 01到31 |
d | 星期中的第几天,数字表示 | 0到6,0表示周日,6表示周六 |
ddd | 三个字母表示星期中的第几天 | Sun到Sat |
dddd | 星期几,完整的星期文本 | 从Sunday到Saturday |
w | 年份中的第几周 | 如42:表示第42周 |
YYYY | 四位数字完整表示的年份 | 如:2014 或 2000 |
YY | 两位数字表示的年份 | 如:14 或 98 |
A | 大写的AM PM | AM PM |
a | 小写的am pm | am pm |
HH | 小时,24小时制,有前导零 | 00到23 |
H | 小时,24小时制,无前导零 | 0到23 |
hh | 小时,12小时制,有前导零 | 00到12 |
h | 小时,12小时制,无前导零 | 0到12 |
m | 没有前导零的分钟数 | 0到59 |
mm | 有前导零的分钟数 | 00到59 |
s | 没有前导零的秒数 | 1到59 |
ss | 有前导零的描述 | 01到59 |
X | Unix时间戳 | 1411572969 |
当前时间 1.0.0+
moment();
获取当前时间,方法等同于 new Date()
var now = moment();
字符串 1.0.0+
moment(String);
When creating a moment from a string, we first check if the string matches known ISO 8601 formats,
then fall back to
new Date(string)if a known format is not found.
var day = moment("1995-12-25");
Warning: Browser support for parsing strings is inconsistent. Because there
is no specification on which formats should be supported, what works in some browsers will not work in other browsers.
For consistent results parsing anything other than ISO 8601 strings, you should use String
+ Format.
Supported ISO 8601 strings
An ISO 8601 string requires a date part.2013-02-08 # A calendar date part 2013-W06-5 # A week date part 2013-039 # An ordinal date part
A time part can also be included, separated from the date part by a space or a uppercase T.
2013-02-08T09 # An hour time part separated by a T 2013-02-08 09 # An hour time part separated by a space 2013-02-08 09:30 # An hour and minute time part 2013-02-08 09:30:26 # An hour, minute, and second time part 2013-02-08 09:30:26.123 # An hour, minute, second, and millisecond time part 2013-02-08 24:00:00.000 # hour 24, minute, second, millisecond equal 0 means next day at midnight
Any of the date parts can have a time part.
2013-02-08 09 # A calendar date part and hour time part 2013-W06-5 09 # A week date part and hour time part 2013-039 09 # An ordinal date part and hour time part
If a time part is included, an offset from UTC can also be included as
+-HH:mm,
+-HHmm,
or
Z.
2013-02-08 09+07:00 # +-HH:mm 2013-02-08 09-0100 # +-HHmm 2013-02-08 09Z # Z 2013-02-08 09:30:26.123+07:00 # +-HH:mm
Note: Automatic cross browser ISO-8601 support was added in version 1.5.0. Support for the week and ordinal formats was added in version 2.3.0.
If a string does not match any of the above formats and is not able to be parsed with
Date.parse,
moment#isValidwill
return false.
moment("not a real date").isValid(); // fals e
字符串 + 格式 1.0.0+
编辑moment(String, String); moment(String, String, String); moment(String, String, Boolean); moment(String, String, String, Boolean);
If you know the format of an input string, you can use that to parse a moment.
moment("12-25-1995", "MM-DD-YYYY");
The parser ignores non-alphanumeric characters, so both of the following will return the same thing.
moment("12-25-1995", "MM-DD-YYYY"); moment("12/25/1995", "MM-DD-YYYY");
The parsing tokens are similar to the formatting tokens used in
moment#format.
Year, month, and day tokens
Input | Example | Description |
---|---|---|
YYYY | 2014 | 4 or 2 digit year |
YY | 14 | 2 digit year |
Q | 1..4 | Quarter of year. Sets month to first month in quarter. |
M MM | 1..12 | Month number |
MMM MMMM | Jan..December | Month name in locale set by moment.locale() |
D DD | 1..31 | Day of month |
Do | 1st..31st | Day of month with ordinal |
DDD DDDD | 1..365 | Day of year |
X | 1410715640.579 | Unix timestamp |
x | 1410715640579 | Unix ms timestamp |
YYYYfrom version
2.10.5supports
2 digit years, and converts them to a year near 2000 (same as
YY).
Week year, week, and weekday tokens
For these, the lowercase tokens use the locale aware week start days, and the uppercase tokens use the ISOweek date start days.
Input | Example | Description |
---|---|---|
gggg | 2014 | Locale 4 digit week year |
gg | 14 | Locale 2 digit week year |
w ww | 1..53 | Locale week of year |
e | 1..7 | Locale day of week |
ddd dddd | Mon...Sunday | Day name in locale set by moment.locale() |
GGGG | 2014 | ISO 4 digit week year |
GG | 14 | ISO 2 digit week year |
W WW | 1..53 | ISO week of year |
E | 1..7 | ISO day of week |
Hour, minute, second, millisecond, and offset tokens
Input | Example | Description |
---|---|---|
H HH | 0..23 | 24 hour time |
h hh | 1..12 | 12 hour time used with a A. |
a A | am pm | Post or ante meridiem |
m mm | 0..59 | Minutes |
s ss | 0..59 | Seconds |
S | 0..9 | Tenths of a second |
SS | 0..99 | Hundreds of a second |
SSS | 0..999 | Thousandths of a second |
SSSS | 0000..9999 | fractional seconds |
Z ZZ | +12:00 | Offset from UTC as +-HH:mm, +-HHmm, or Z |
digits and want to consume the input.
Locale aware date and time formats are also available using
LT LTS L LL LLL LLLL. They were added in version 2.2.1,
except
LTSwhich was added 2.8.4.
Z ZZwere added in version
1.2.0.
S SS SSSwere added in version
1.6.0.
Xwas added in version
2.0.0.
Unless you specify a timezone offset, parsing a string will create a date in the current timezone.
moment("2010-10-20 4:30", "YYYY-MM-DD HH:mm"); // parsed as 4:30 local time moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC
If the moment that results from the parsed input does not exist,
moment#isValidwill return false.
moment("2010 13", "YYYY MM").isValid(); // false (not a real month) moment("2010 11 31", "YYYY MM DD").isValid(); // false (not a real day) moment("2010 2 29", "YYYY MM DD").isValid(); // false (not a leap year) moment("2010 notamonth 29", "YYYY MMM DD").isValid(); // false (not a real month name)
As of version
2.0.0, a locale key can be passed as the third parameter to
moment()and
moment.utc().
moment('2012 juillet', 'YYYY MMM', 'fr'); moment('2012 July', 'YYYY MMM', 'en');
Moment's parser is very forgiving, and this can lead to undesired behavior. As of version
2.3.0, you may specify a boolean
for the last argument to make Moment use strict parsing. Strict parsing requires that the format and input match exactly.
moment('It is 2012-05-25', 'YYYY-MM-DD').isValid(); // true moment('It is 2012-05-25', 'YYYY-MM-DD', true).isValid(); // false moment('2012-05-25', 'YYYY-MM-DD', true).isValid(); // true
You can use both locale and strictness.
moment('2012-10-14', 'YYYY-MM-DD', 'fr', true);
Parsing two digit years
By default, two digit years above 68 are assumed to be in the 1900's and years 68 or below are assumed to be in the 2000's. This can be changed by replacing the moment.parseTwoDigitYearmethod.
字符串 + 多个格式 1.0.0+
编辑moment(String, String[], String, Boolean);
If you don't know the exact format of an input string, but know it could be one of many, you can use an array of formats.
This is the same as String + Format, only it will try to match the input to multiple formats.
moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]);
Starting in version
2.3.0, Moment uses some simple heuristics to determine which format to use. In order:
Prefer formats resulting in valid dates over invalid ones.
Prefer formats that parse more of the string than less and use more of the format than less, i.e. prefer stricter parsing.
Prefer formats earlier in the array than later.
moment("29-06-1995", ["MM-DD-YYYY", "DD-MM", "DD-MM-YYYY"]); // uses the last format moment("05-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"]); // uses the first format
You may also specify a locale and strictness argument. They work the same was as they do in the single format case.
moment("29-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"], 'fr'); // uses 'fr' locale moment("29-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"], true); // uses strict parsing moment("05-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"], 'fr', true); // uses 'fr' locale and strict parsing
Note: Parsing multiple formats is considerably slower than parsing a single format. If you can avoid it, it is much faster to parse a single format.
字符串特殊格式 2.7.0+
编辑moment(String, moment.CUSTOM_FORMAT, [String], [Boolean]); moment(String, [..., moment.ISO_8601, ...], [String], [Boolean]);
ISO-8601 is a standard for time and duration display. Moment already supports parsing iso-8601 strings,
but this can be specified explicitly in the format/list of formats when constructing a moment.
To specify iso-8601 parsing use
moment.ISO_8601constant. More formats will be added in the future.
Examples:
moment("2010-01-01T05:06:07", moment.ISO_8601); moment("2010-01-01T05:06:07", ["YYYY", moment.ISO_8601]);
对象 2.2.1+
编辑moment({unit: value, ...});
moment({ hour:15, minute:10 }); moment({ y :2010, M :3, d :5, h :15, m :10, s :3, ms :123}); moment({ year :2010, month :3, day :5, hour :15, minute :10, second :3, millisecond :123}); moment({ years:2010, months:3, days:5, hours:15, minutes:10, seconds:3, milliseconds:123}); moment({ years:2010, months:3, date:5, hours:15, minutes:10, seconds:3, milliseconds:123});
You can create a moment by specifying some of the units in an object.
Omitted units default to 0 or the current date, month, and year.
dayand
datekey both mean
day-of-the-month.
datewas added in 2.8.4.
Note that like
moment(Array)and
new Date(year, month, date), months are 0 indexed.
Unix 偏移量 (毫秒) 1.0.0+
编辑moment(Number);
Similar to
new Date(Number), you can create a moment by passing an integer value representing the number of milliseconds since
the Unix Epoch (Jan 1 1970 12AM UTC).
var day = moment(1318781876406);
Unix 时间戳 (秒) 1.6.0+
编辑moment.unix(Number)
To create a moment from a Unix timestamp (seconds since the Unix Epoch), use
moment.unix(Number).
var day = moment.unix(1318781876);
This is implemented as
moment(timestamp * 1000), so partial seconds in the input timestamp are included.
var day = moment.unix(1318781876.721);
Date 对象 1.0.0+
编辑moment(Date);
You can create a
Momentwith a pre-existing native Javascript
Dateobject.
var day = new Date(2011, 9, 16); var dayWrapper = moment(day);
This clones
Dateobject; further changes to the
Datewon't
affect the
Moment, and vice-versa.
数组 1.0.0+
编辑moment(Number[]);
You can create a moment with an array of numbers that mirror the parameters passed to new
Date()
[year, month, day, hour, minute, second, millisecond] moment([2010, 1, 14, 15, 25, 50, 125]); // February 14th, 3:25:50.125 PM
Any value past the year is optional, and will default to the lowest possible number.
moment([2010]); // January 1st moment([2010, 6]); // July 1st moment([2010, 6, 10]); // July 10th
Construction with an array will create a date in the current timezone. To create a date from an array at UTC, use
moment.utc(Number[]).
moment.utc([2010, 1, 14, 15, 25, 50, 125]);
Note: Because this mirrors the native
Dateparameters, months, hours, minutes, seconds, and milliseconds
are all zero indexed. Years and days of the month are 1 indexed.
This is often the cause of frustration, especially with months, so take note!
If the date represented by the array does not exist,
moment#isValidwill return false.
moment([2010, 13]).isValid(); // false (not a real month) moment([2010, 10, 31]).isValid(); // false (not a real day) moment([2010, 1, 29]).isValid(); // false (not a leap year)
ASP.NET 1.3.0+
编辑moment(String);
Microsoft Web API returns JSON dates in proper ISO-8601 format by default, but older ASP.NET technologies may return dates in JSON as
/Date(1198908717056)/or
/Date(1198908717056-0700)/
If a string that matches this format is passed in, it will be parsed correctly.
moment("/Date(1198908717056-0700)/"); // 2007-12-28T23:11:57.056-07:00
克隆 1.2.0+
编辑moment(Moment);
All moments are mutable. If you want a clone of a moment, you can do so explicitly or implicitly.
Calling
moment()on a moment will clone it.
var a = moment([2012]); var b = moment(a); a.year(2000); b.year(); // 2012
Additionally, you can call
moment#cloneto clone a moment.
var a = moment([2012]); var b = a.clone(); a.year(2000); b.year(); // 2012
UTC 1.5.0+
编辑moment.utc(); moment.utc(Number); moment.utc(Number[]); moment.utc(String); moment.utc(String, String); moment.utc(String, String[]); moment.utc(String, String, String); moment.utc(Moment); moment.utc(Date);
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use
moment.utc()instead of
moment().
This brings us to an interesting feature of Moment.js. UTC mode.
While in UTC mode, all display methods will display in UTC time instead of local time.
moment().format(); // 2013-02-04T10:35:24-08:00 moment.utc().format(); // 2013-02-04T18:35:24+00:00
Additionally, while in UTC mode, all getters and setters will internally use the
Date#getUTC*and
Date#setUTC*methods
instead of the
Date#get*and
Date#set*methods.
moment.utc().seconds(30) === new Date().setUTCSeconds(30); moment.utc().seconds() === new Date().getUTCSeconds();
It is important to note that though the displays differ above, they are both the same moment in time.
var a = moment(); var b = moment.utc(); a.format(); // 2013-02-04T10:35:24-08:00 b.format(); // 2013-02-04T18:35:24+00:00 a.valueOf(); // 1360002924000 b.valueOf(); // 1360002924000
Any moment created with
moment.utc()will be in UTC mode, and any moment created with
moment()will
not.
To switch from UTC to local time, you can use moment#utc or moment#local.
var a = moment.utc([2011, 0, 1, 8]); a.hours(); // 8 UTC a.local(); a.hours(); // 0 PST
时区 2.3.0+
编辑moment.parseZone(String)
Moment normally interprets input times as local times (or UTC times if
moment.utc()is used). However, often the input
string itself contains time zone information.
#parseZoneparses the time and then sets the zone according to the input string.
moment.parseZone("2013-01-01T00:00:00-13:00").zone(); // 780
moment.parseZoneis equivalent to parsing the string and using
moment#zoneto
parse the zone.
var s = "2013-01-01T00:00:00-13:00"; moment(s).zone(s);
Note: this method only works for a single string argument, not a string and format.
合法性校验 1.7.0+
编辑moment().isValid();
Moment applies stricter initialization rules than the
Dateconstructor.
new Date(2013, 25, 14).toString(); // "Sat Feb 14 2015 00:00:00 GMT-0500 (EST)" moment([2015, 25, 35]).format(); // 'Invalid date'
You can check whether the Moment considers the date invalid using
moment#isValid. You can check the metrics used by
#isValidusing
moment#parsingFlags,
which returns an object.
The following parsing flags result in an invalid date:
overflow: An overflow of a date field, such as a 13th month, a 32nd day of the month (or a 29th of February on non-leap years), a 367th day of the year,
etc.
overflowcontains the index of the invalid unit to match
#invalidAt(see
below);
-1means no overflow.
invalidMonth: An invalid month name, such as
moment('Marbruary', 'MMMM');.
Contains the invalid month string itself, or else null.
empty: An input string that contains nothing parsable, such as
moment('this is nonsense');. Boolean.
nullInput: A
nullinput, like
moment(null);.
Boolean.
invalidFormat: An empty list of formats, such as
moment('2013-05-25', []). Boolean.
userInvalidated: A date created explicitly as invalid, such as
moment.invalid().
Boolean.
Additionally, if the Moment is parsed in strict mode, these flags must be empty for the Moment to be valid:
unusedTokens: array of format substrings not found in the input string
unusedInput: array of input substrings not matched to the format string
Note: Moment's concept of validity became more strict and consistent between 2.2 and 2.3.
Additionally, you can use
moment#invalidAtto determine which date unit overflowed.
var m = moment("2011-10-10T10:20:90"); m.isValid(); // false m.invalidAt(); // 5 for seconds
The return value has the following meaning:
years
months
days
hours
minutes
seconds
milliseconds
Note: In case of multiple wrong units the first one is returned (because days validity may depend on month, for example).
默认值 2.2.1+
编辑moment("15", "hh")
You can create a moment object specifying only some of the units, and the rest will be defaulted to the current day, month or year, or 0 for hours, minutes, seconds and milliseconds.
Defaulting to now, when nothing is passed:
moment(); // current date and time
Defaulting to today, when only hours, minutes, seconds and milliseconds are passed:
moment(5, "HH"); // today, 5:00:00.000 moment({hour: 5}); // today, 5:00:00.000 moment({hour: 5, minute: 10}); // today, 5:10.00.000 moment({hour: 5, minute: 10, seconds: 20}); // today, 5:10.20.000 moment({hour: 5, minute: 10, seconds: 20, milliseconds: 300}); // today, 5:10.20.300
Defaulting to this month and year, when only days and smaller units are passed:
moment(5, "DD"); // this month, 5th day-of-month moment("4 05:06:07", "DD hh:mm:ss"); // this month, 4th day-of-month, 05:06:07.000
Defaulting to this year, if year is not specified:
moment(3, "MM"); // this year, 3th month (April) moment("Apr 4 05:06:07", "MMM DD hh:mm:ss"); // this year, 5th April, 05:06:07.000
取值/赋值
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
Dateobject.
moment().seconds(30) === new Date().setSeconds(30); moment().seconds() === new Date().getSeconds();
If you are in UTC mode, they will map to the UTC equivalent.
moment.utc().seconds(30) === new Date().setUTCSeconds(30); moment.utc().seconds() === new Date().getUTCSeconds();
For convenience, both singular and plural method names exist as of version
2.0.0.
Note: All of these methods mutate the original moment when used as setters.
毫秒 1.3.0+
编辑moment().millisecond(Number); moment().millisecond(); // Number moment().milliseconds(Number); moment().milliseconds(); // Number
Gets or sets the milliseconds.
Accepts numbers from 0 to 999. If the range is exceeded, it will bubble up to the seconds.
秒 1.0.0+
编辑moment().second(Number); moment().second(); // Number moment().seconds(Number); moment().seconds(); // Number
Gets or sets the seconds.
Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the minutes.
分钟 1.0.0+
编辑moment().minute(Number); moment().minute(); // Number moment().minutes(Number); moment().minutes(); // Number
Gets or sets the minutes.
Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the hours.
小时 1.0.0+
编辑moment().hour(Number); moment().hour(); // Number moment().hours(Number); moment().hours(); // Number
Gets or sets the hour.
Accepts numbers from 0 to 23. If the range is exceeded, it will bubble up to the day.
日期 1.0.0+
编辑moment().date(Number); moment().date(); // Number moment().dates(Number); moment().dates(); // Number
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.
Note:
Moment#dateis for the date of the month, and
Moment#dayis
for the day of the week.
星期 1.3.0+
编辑moment().day(Number|String); moment().day(); // Number moment().days(Number|String); moment().days(); // Number
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.
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)
Note:
Moment#dateis for the date of the month, and
Moment#dayis
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.
moment().day("Sunday"); moment().day("Monday");
星期 (区域标准) 2.1.0+
编辑moment().weekday(Number); moment().weekday(); // Number
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.
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
星期 (ISO) 2.1.0+
编辑moment().isoWeekday(Number); moment().isoWeekday(); // Number
Gets or sets the ISO day of the week with
1being
Monday and
7being Sunday.
moment().isoWeekday(1); // Monday moment().isoWeekday(7); // Sunday
天 2.0.0+
编辑moment().dayOfYear(Number); moment().dayOfYear(); // Number
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.
周 2.0.0+
编辑moment().week(Number); moment().week(); // Number moment().weeks(Number); moment().weeks(); // Number
Gets or sets the week of the year.
Because different locales define week of year numbering differently, Moment.js added
moment#weekto 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#weekwill depend on the locale for
that moment.
When setting the week of the year, the day of the week is retained.
周 (ISO) 2.0.0+
编辑moment().isoWeek(Number); moment().isoWeek(); // Number moment().isoWeeks(Number); moment().isoWeeks(); // Number
Gets or sets the ISO week of the year.
When setting the week of the year, the day of the week is retained.
月 1.0.0+
编辑moment().month(Number|String); moment().month(); // Number moment().months(Number|String); moment().months(); // Number
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.
moment().month("January"); moment().month("Feb");
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.
// 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.6.0+
编辑moment().quarter(); // Number moment().quarter(Number);
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
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'
年 1.0.0+
编辑moment().year(Number); moment().year(); // Number moment().years(Number); moment().years(); // Number
Gets or sets the year.
Accepts numbers from -270,000 to 270,000.
周 (区域标准) 2.1.0+
编辑moment().weekYear(Number); moment().weekYear(); // Number
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.
周 (ISO) 2.1.0+
编辑moment().isoWeekYear(Number); moment().isoWeekYear(); // Number
Gets or sets the ISO week-year.
周数 2.6.0+
编辑moment().weeksInYear();
Gets the number of weeks according to locale in the current moment's year.
周数 (ISO) 2.6.0+
编辑moment().isoWeeksInYear();
Gets the number of weeks in the current moment's year, according to ISO weeks.
取值 2.2.1+
编辑moment().get('year'); moment().get('month'); // 0 to 11 moment().get('date'); moment().get('hour'); moment().get('minute'); moment().get('second'); moment().get('millisecond');
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).
赋值 2.2.1+
编辑moment().set(String, Int); moment().set(Object(String, Int));
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});
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
最大值 2.7.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
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.7.0+
编辑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
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
操作
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().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 use
moment#clonebefore manipulating the moment. More
info on cloning.
加法 1.0.0+
编辑moment().add(Number, String); moment().add(Duration); moment().add(Object);
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().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 |
moment().add(7, 'days').add(1, 'months'); // with chaining moment().add({days:7,months:1}); // with object literal
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
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
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
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
Alternatively, you can use durations to add to moments.
var duration = moment.duration({'days' : 1}); moment([2012, 0, 31]).add(duration); // February 1
Before version
2.8.0, the
moment#add(String, Number)syntax was also supported. It has been deprecated in favor of
moment#add(Number, String).
moment().add('seconds', 1); // Deprecated in 2.8.0 moment().add(1, 'seconds');
减法 1.0.0+
编辑moment().subtract(Number, String); moment().subtract(Duration); moment().subtract(Object);
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');
开始时间 1.7.0+
编辑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);
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)
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.
结束时间 1.7.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.mininstead.
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
maxis 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():
This is the counterpart for
moment#min.
var momentFromServer = moment(input); var clampedMoment = momentFromServer.max();
You can pass anything to
moment#maxthat 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));
最小值 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.maxinstead.
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
minis 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#maxto clamp a moment to a range.
var start = moment().startOf('week'); var end = moment().endOf('week'); var actual = moment().min(start).max(end);
本地化 1.5.0+
编辑moment().local();
Sets a flag on the original moment to internally use
Date#get*and
Date#set*instead
of
Date#getUTC*and
Date#setUTC*.
var a = moment.utc([2011, 0, 1, 8]); a.hours(); // 8 UTC a.local(); a.hours(); // 0 PST
See moment.utc() for more information on UTC mode.
UTC 1.5.0+
编辑moment().utc();
Sets a flag on the original moment to internally use
Date#getUTC*and
Date#setUTC*instead
of
Date#get*and
Date#set*.
var a = moment([2011, 0, 1, 8]); a.hours(); // 8 PST a.utc(); a.hours(); // 16 UTC
See moment.utc() for more information on UTC mode.
UTC 偏移量 2.9.0++
编辑moment().utcOffset(); moment().utcOffset(Number|String);
Get the utc offset in minutes.
NOTE: Unlike
moment.fn.zonethis
function returns the real offset from UTC, not the reverse offset (as returned by
Date.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.
moment().utcOffset(120);
If the input is less than
16and greater than
-16,
it will interpret your input as hours instead.
// these are equivalent moment().utcOffset(8); // set hours offset moment().utcOffset(480); // set minutes offset (8 * 60)
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);
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".
moment().utcOffset("2013-03-07T07:00:00+08:00");
时区偏移量 From 1.2.0, deprecated 2.9.0+
编辑moment().zone(); moment().zone(Number|String);
NOTE: This function has been deprecated in 2.9.0. Consider
moment.fn.utcOffsetinstead.
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
16and greater than
-16,
it will interpret your input as hours instead.
// these are equivalent moment().zone(480); moment().zone(8);
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");
显示
Once parsing and manipulation are done, you need some way to display the moment.
格式化 1.0.0+
编辑moment().format(); moment().format(String);
This is the most robust display option. It takes a string of tokens and replaces them with their corresponding values.
moment().format(); // "2014-09-08T08:02:17-05:00" (ISO 8601) moment().format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm" moment().format("ddd, hA"); // "Sun, 3PM" moment('gibberish').format('YYYY MM DD'); // "Invalid date"
Token | Output | |
---|---|---|
Month | M | 1 2 ... 11 12 |
Mo | 1st 2nd ... 11th 12th | |
MM | 01 02 ... 11 12 | |
MMM | Jan Feb ... Nov Dec | |
MMMM | January February ... November December | |
Quarter | Q | 1 2 3 4 |
Day of Month | D | 1 2 ... 30 31 |
Do | 1st 2nd ... 30th 31st | |
DD | 01 02 ... 30 31 | |
Day of Year | DDD | 1 2 ... 364 365 |
DDDo | 1st 2nd ... 364th 365th | |
DDDD | 001 002 ... 364 365 | |
Day of Week | d | 0 1 ... 5 6 |
do | 0th 1st ... 5th 6th | |
dd | Su Mo ... Fr Sa | |
ddd | Sun Mon ... Fri Sat | |
dddd | Sunday Monday ... Friday Saturday | |
Day of Week (Locale) | e | 0 1 ... 5 6 |
Day of Week (ISO) | E | 1 2 ... 6 7 |
Week of Year | w | 1 2 ... 52 53 |
wo | 1st 2nd ... 52nd 53rd | |
ww | 01 02 ... 52 53 | |
Week of Year (ISO) | W | 1 2 ... 52 53 |
Wo | 1st 2nd ... 52nd 53rd | |
WW | 01 02 ... 52 53 | |
Year | YY | 70 71 ... 29 30 |
YYYY | 1970 1971 ... 2029 2030 | |
Week Year | gg | 70 71 ... 29 30 |
gggg | 1970 1971 ... 2029 2030 | |
Week Year (ISO) | GG | 70 71 ... 29 30 |
GGGG | 1970 1971 ... 2029 2030 | |
AM/PM | A | AM PM |
a | am pm | |
Hour | H | 0 1 ... 22 23 |
HH | 00 01 ... 22 23 | |
h | 1 2 ... 11 12 | |
hh | 01 02 ... 11 12 | |
Minute | m | 0 1 ... 58 59 |
mm | 00 01 ... 58 59 | |
Second | s | 0 1 ... 58 59 |
ss | 00 01 ... 58 59 | |
Fractional Second | S | 0 1 ... 8 9 |
SS | 00 01 ... 98 99 | |
SSS | 000 001 ... 998 999 | |
SSSS ... SSSSSSSSS | 000[0..] 001[0..] ... 998[0..] 999[0..] | |
Timezone | z or zz | EST CST ... MST PST Note: as of 1.6.0, the z/zz format tokens have been deprecated. Read more about it here. |
Z | -07:00 -06:00 ... +06:00 +07:00 | |
ZZ | -0700 -0600 ... +0600 +0700 | |
Unix Timestamp | X | 1360013296 |
Unix Millisecond Timestamp | x | 1360013296123 |
Z ZZwere added in 1.2.0.
S SS SSSwere added in 1.6.0.
Xwas added in 2.0.0.
e E gg gggg GG GGGGwere added in 2.1.0.
xwas added in 2.8.4.
SSSSto
SSSSSSSSSwere added
in 2.10.5. They display 3 significant digits and the rest is filled with zeros.
Localized formats
Because preferred formatting differs based on locale, there are a few tokens that can be used to format a moment based on its locale.There are upper and lower case variations on the same formats. The lowercase version is intended to be the shortened version of its uppercase counterpart.
Time | LT | 8:30 PM |
Time with seconds | LTS | 8:30:25 PM |
Month numeral, day of month, year | L | 09/04/1986 |
l | 9/4/1986 | |
Month name, day of month, year | LL | September 4 1986 |
ll | Sep 4 1986 | |
Month name, day of month, year, time | LLL | September 4 1986 8:30 PM |
lll | Sep 4 1986 8:30 PM | |
Month name, day of month, day of week, year, time | LLLL | Thursday, September 4 1986 8:30 PM |
llll | Thu, Sep 4 1986 8:30 PM |
L LL LLL LLLL LTare available in version 1.3.0.
l ll lll llllare available in 2.0.0.
LTSwas added in 2.8.4.
Escaping characters
To escape characters in format strings, you can wrap the characters in square brackets.moment().format('[today] dddd'); // 'today Sunday'
Similarities and differences with LDML
Note: While these date formats are very similar to LDML date formats, there are a few minor differences regarding day of month, day of year, and day of week.For a breakdown of a few different date formatting tokens across different locales, see this
chart of date formatting tokens.
Formatting speed
To compare Moment.js formatting speed against other libraries, check out this comparison against other libraries.
Other tokens
If you are more comfortable working with strftime instead of LDML-like parsing tokens, you can use Ben Oakes' plugin. benjaminoakes/moment-strftime.
Default format
As of version 1.5.0, calling moment#formatwithout a format will default to
moment.defaultFormat.
Out of the box,
moment.defaultFormatis the ISO8601 format
YYYY-MM-DDTHH:mm:ssZ.
时差 (之前,现在为基准) 1.0.0+
编辑moment().fromNow(); moment().fromNow(Boolean);
A common way of displaying time is handled by
moment#fromNow. This is sometimes called timeago or relative time.
moment([2007, 0, 29]).fromNow(); // 4 years ago
If you pass
true, you can get the value without the suffix.
moment([2007, 0, 29]).fromNow(); // 4 years ago moment([2007, 0, 29]).fromNow(true); // 4 years
The base strings are customized by the current locale.
The breakdown of which string is displayed for each length of time is outlined in the table below.
Range | Key | Sample Output |
---|---|---|
0 to 45 seconds | s | a few seconds ago |
45 to 90 seconds | m | a minute ago |
90 seconds to 45 minutes | mm | 2 minutes ago ... 45 minutes ago |
45 to 90 minutes | h | an hour ago |
90 minutes to 22 hours | hh | 2 hours ago ... 22 hours ago |
22 to 36 hours | d | a day ago |
36 hours to 25 days | dd | 2 days ago ... 25 days ago |
25 to 45 days | M | a month ago |
45 to 345 days | MM | 2 months ago ... 11 months ago |
345 to 545 days (1.5 years) | y | a year ago |
546 days+ | yy | 2 years ago ... 20 years ago |
时差 (之前) 1.0.0+
编辑moment().from(Moment|String|Number|Date|Array); moment().from(Moment|String|Number|Date|Array, Boolean);
You may want to display a moment in relation to a time other than now. In that case, you can use
moment#from.
var a = moment([2007, 0, 28]); var b = moment([2007, 0, 29]); a.from(b) // "a day ago"
The first parameter is anything you can pass to
moment()or an actual
Moment.
var a = moment([2007, 0, 28]); var b = moment([2007, 0, 29]); a.from(b); // "a day ago" a.from([2007, 0, 29]); // "a day ago" a.from(new Date(2007, 0, 29)); // "a day ago" a.from("2007-01-29"); // "a day ago"
Like
moment#fromNow, passing
trueas
the second parameter returns value without the suffix. This is useful wherever you need to have a human readable length of time.
var start = moment([2007, 0, 5]); var end = moment([2007, 0, 10]); end.from(start); // "in 5 days" end.from(start, true); // "5 days"
From version 2.10.3, if any of the endpoints are invalid the result is the localized Invalid date string.
时差 (之后,现在为基准) 2.10.3+
编辑moment().toNow(); moment().toNow(Boolean);
A common way of displaying time is handled by
moment#toNow. This is sometimes called timeago or relative time.
This is similar to
moment.fromNow,
but gives the opposite interval:
a.fromNow() = - a.toNow().
This is similar to
moment.to,
but is special-cased for the current time. Use
moment.to, if you want to control the two end points of the interval.
moment([2007, 0, 29]).toNow(); // in 4 years
If you pass
true, you can get the value without the prefix.
moment([2007, 0, 29]).toNow(); // in 4 years moment([2007, 0, 29]).toNow(true); // 4 years
The base strings are customized by the current locale.
The breakdown of which string is displayed for each length of time is outlined in the table below.
Range | Key | Sample Output |
---|---|---|
0 to 45 seconds | s | in seconds |
45 to 90 seconds | m | in a minute |
90 seconds to 45 minutes | mm | in 2 minutes ... in 45 minutes |
45 to 90 minutes | h | in an hour |
90 minutes to 22 hours | hh | in 2 hours ... in 22 hours |
22 to 36 hours | d | in a day |
36 hours to 25 days | dd | in 2 days ... in 25 days |
25 to 45 days | M | in a month |
45 to 345 days | MM | in 2 months ... in 11 months |
345 to 547 days (1.5 years) | y | in a year |
548 days+ | yy | in 2 years ... in 20 years |
时差 (之后) 2.10.3+
编辑moment().to(Moment|String|Number|Date|Array); moment().to(Moment|String|Number|Date|Array, Boolean);
You may want to display a moment in relation to a time other than now. In that case, you can use
moment#to.
var a = moment([2007, 0, 28]); var b = moment([2007, 0, 29]); a.to(b) // "in a day"
The first parameter is anything you can pass to
moment()or an actual
Moment.
var a = moment([2007, 0, 28]); var b = moment([2007, 0, 29]); a.to(b); // "in a day" a.to([2007, 0, 29]); // "in a day" a.to(new Date(2007, 0, 29)); // "in a day" a.to("2007-01-29"); // "in a day"
Like
moment#toNow, passing
trueas
the second parameter returns value without the suffix. This is useful wherever you need to have a human readable length of time.
var start = moment([2007, 0, 5]); var end = moment([2007, 0, 10]); end.to(start); // "5 days ago" end.to(start, true); // "5 days"
From version 2.10.3, if any of the endpoints are invalid the result is the localized Invalid date string.
日历时间 1.3.0+
编辑moment().calendar(); moment().calendar(referenceTime); moment().calendar(referenceTime, formats); // from 2.10.5
Calendar time displays time relative to a given
referenceTime(defaults to now), but does so slightly differently than
moment#fromNow.
moment#calendarwill format a date with different strings depending on how close to
referenceTime's
date (today by default) the date is.
Last week | Last Monday at 2:30 AM |
The day before | Yesterday at 2:30 AM |
The same day | Today at 2:30 AM |
The next day | Tomorrow at 2:30 AM |
The next week | Sunday at 2:30 AM |
Everything else | 7/10/2011 |
From 2.10.5 moment supports specifying calendar output formats per invocation:
moment().calendar(null, { sameDay: '[Today]', nextDay: '[Tomorrow]', nextWeek: 'dddd', lastDay: '[Yesterday]', lastWeek: '[Last] dddd', sameElse: 'DD/MM/YYYY' });
sameElseis used as the format when the moment is more than a week away from the
referenceTime
时差 (毫秒) 1.0.0+
编辑moment().diff(Moment|String|Number|Date|Array); moment().diff(Moment|String|Number|Date|Array, String); moment().diff(Moment|String|Number|Date|Array, String, Boolean);
To get the difference in milliseconds, use
moment#difflike you would use
moment#from.
var a = moment([2007, 0, 29]); var b = moment([2007, 0, 28]); a.diff(b) // 86400000
To get the difference in another unit of measurement, pass that measurement as the second argument.
var a = moment([2007, 0, 29]); var b = moment([2007, 0, 28]); a.diff(b, 'days') // 1
The supported measurements are years, months, weeks, days, hours, minutes, and seconds. For ease of development, the singular forms are supported as of 2.0.0. Units of measurement other than
milliseconds are available in version 1.1.1.
By default,
moment#diffwill return number rounded down. If you want the floating point number, pass
trueas
the third argument. Before 2.0.0,
moment#diffreturned rounded number, not a rounded downnumber.
var a = moment([2008, 6]); var b = moment([2007, 0]); a.diff(b, 'years'); // 1 a.diff(b, 'years', true); // 1.5
If the moment is earlier than the moment you are passing to
moment.fn.diff, the return value will be negative.
var a = moment(); var b = moment().add(1, 'seconds'); a.diff(b) // -1000 b.diff(a) // 1000
A easy way to think of this is by replacing
.diff(with a minus operator.
// a < b a.diff(b) // a - b < 0 b.diff(a) // b - a > 0
Month and year diffs
moment#diffhas some special handling for month and year diffs. It is optimized to ensure that two months with the same
date are always a whole number apart.
So Jan 15 to Feb 15 should be exactly 1 month.
Feb 28 to Mar 28 should be exactly 1 month.
Feb 28 2011 to Feb 28 2012 should be exactly 1 year.
See more discussion on the month and year diffs here
This change to month and year diffs was made in 2.0.0. As of version 2.9.0 diff also support quarter unit.
Unix 偏移量 (毫秒) 1.0.0+
编辑moment().valueOf(); +moment();
moment#valueOfsimply outputs the number of milliseconds since the Unix Epoch, just like
Date#valueOf.
moment(1318874398806).valueOf(); // 1318874398806 +moment(1318874398806); // 1318874398806
To get a Unix timestamp (the number of seconds since the epoch) from a
Moment, use
moment#unix.
Unix 时间戳 (秒) 1.6.0+
编辑moment().unix();
moment#unixoutputs a Unix timestamp (the number of seconds since the Unix Epoch).
moment(1318874398806).unix(); // 1318874398
This value is floored to the nearest second, and does not include a milliseconds component.
天数 (月) 1.5.0+
编辑moment().daysInMonth();
Get the number of days in the current month.
moment("2012-02", "YYYY-MM").daysInMonth() // 29 moment("2012-01", "YYYY-MM").daysInMonth() // 31
Date 对象 1.0.0+
编辑moment().toDate();
To get the native Date object that Moment.js wraps, use
moment#toDate.
This will return the
Datethat the moment uses, so any changes to that
Datewill
cause the moment to change. If you want a
Datethat is a copy, use
moment#clonebefore
you use
moment#toDate.
moment#nativehas been replaced by
moment#toDateand
has been deprecated as of 1.6.0.
数组 1.7.0+
编辑moment().toArray();
This returns an array that mirrors the parameters from
new Date().
moment().toArray(); // [2013, 1, 4, 14, 40, 16, 154];
JSON 2.0.0+
编辑moment().toJSON();
When serializing an object to JSON, if there is a
Momentobject, it will be represented as an ISO8601 string, adjusted
to UTC.
JSON.stringify({ postDate : moment() }); // '{"postDate":"2013-02-04T22:44:30.652Z"}'
If instead you would like an ISO8601 string that reflects the moment's
utcOffset(), then you can modify the
toJSONfunction
like this:
moment.fn.toJSON = function() { return this.format(); }
This changes the behavior as follows:
JSON.stringify({ postDate : moment() }); // '{"postDate":"2013-02-04T14:44:30-08:00"}'
ISO 8601 字符串 2.1.0+
编辑moment().toISOString();
Formats a string to the ISO8601 standard.
moment().toISOString() // 2013-02-04T22:44:30.652Z
From version 2.8.4 the native
Date.prototype.toISOStringis used if available, for performance reasons.
对象 2.10.5+
编辑moment().toObject();
This returns an object containing year, month, day-of-month, hour, minute, seconds, milliseconds.
moment().toObject() // { // years: 2015 // months: 6 // date: 26, // hours: 1, // minutes: 53, // seconds: 14, // milliseconds: 600 // }
查询
是否之前 2.0.0+
编辑moment().isBefore(Moment|String|Number|Date|Array); moment().isBefore(Moment|String|Number|Date|Array, String);
Check if a moment is before another moment.
moment('2010-10-20').isBefore('2010-10-21'); // true
If you want to limit the granularity to a unit other than milliseconds, pass the units as the second parameter.
As the second parameter determines the precision, and not just a single value to check, using day will check for year, month and day.
moment('2010-10-20').isBefore('2010-12-31', 'year'); // false moment('2010-10-20').isBefore('2011-01-01', 'year'); // true
Like
moment#isAfterand
moment#isSame,
any of the units of time that are supported for
moment#startOfare supported for
moment#isBefore.
year month week day hour minute second
If nothing is passed to
moment#isBefore, it will default to the current time.
NOTE:
moment().isBefore()has undefined behavior and should not be used! If the code runs fast the
initial created moment would be the same as the one created in isBefore to perform the check, so the result would be
false. But if the code runs slower it's possible
that the moment created in isBefore is measurably after the one created in
moment(), so the call would return
true.
是否相同 2.0.0+
编辑moment().isSame(Moment|String|Number|Date|Array); moment().isSame(Moment|String|Number|Date|Array, String);
Check if a moment is the same as another moment.
moment('2010-10-20').isSame('2010-10-20'); // true
If you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter.
moment('2010-10-20').isSame('2009-12-31', 'year'); // false moment('2010-10-20').isSame('2010-01-01', 'year'); // true moment('2010-10-20').isSame('2010-12-31', 'year'); // true moment('2010-10-20').isSame('2011-01-01', 'year'); // false
When including a second parameter, it will match all units equal or larger. Passing in
monthwill check
monthand
year.
Passing in
daywill check
day,
month,
and
year.
moment('2010-01-01').isSame('2011-01-01', 'month'); // false, different year moment('2010-01-01').isSame('2010-02-01', 'day'); // false, different month
Like
moment#isAfterand
moment#isBefore,
any of the units of time that are supported for
moment#startOfare supported for
moment#isSame.
year month week day hour minute second
是否之后 2.0.0+
编辑moment().isAfter(Moment|String|Number|Date|Array); moment().isAfter(Moment|String|Number|Date|Array, String);
Check if a moment is after another moment.
moment('2010-10-20').isAfter('2010-10-19'); // true
If you want to limit the granularity to a unit other than milliseconds, pass the units as the second parameter.
As the second parameter determines the precision, and not just a single value to check, using day will check for year, month and day.
moment('2010-10-20').isAfter('2010-01-01', 'year'); // false moment('2010-10-20').isAfter('2009-12-31', 'year'); // true
Like
moment#isSameand
moment#isBefore,
any of the units of time that are supported for
moment#startOfare supported for
moment#isAfter.
year month week day hour minute second
If nothing is passed to
moment#isAfter, it will default to the current time.
moment().isAfter(); // false
是否之间 2.9.0+
编辑moment().isBetween(moment-like, moment-like); moment().isBetween(moment-like, moment-like, String); // where moment-like is Moment|String|Number|Date|Array
Check if a moment is between two other moments, optionally looking at unit scale (minutes, hours, days, etc). The match is exclusive.
moment('2010-10-20').isBetween('2010-10-19', '2010-10-25'); // true
If you want to limit the granularity to a unit other than milliseconds, pass the units as the second parameter.
moment('2010-10-20').isBetween('2010-01-01', '2012-01-01', 'year'); // false moment('2010-10-20').isBetween('2009-12-31', '2012-01-01', 'year'); // true
Like
moment#isSame,
moment#isBefore,
moment#isAfterany
of the units of time that are supported for
moment#startOfare supported for
moment#isBetween.
Year, month, week, day, hour, minute, and second.
是否闰年 1.0.0+
编辑moment().isLeapYear();
moment#isLeapYearreturns
trueif
that year is a leap year, and
falseif it is not.
moment([2000]).isLeapYear() // true moment([2001]).isLeapYear() // false moment([2100]).isLeapYear() // false
是否夏令时 1.2.0+
编辑moment().isDST();
moment#isDSTchecks if the current moment is in daylight saving time.
moment([2011, 2, 12]).isDST(); // false, March 12 2011 is not DST moment([2011, 2, 14]).isDST(); // true, March 14 2011 is DST
是否被夏令时转换 2.3.0+
编辑moment('2013-03-10 2:30', 'YYYY-MM-DD HH:mm').isDSTShifted()
Another important piece of validation is to know if the date has been moved by a DST. For example, in most of the United States:
moment('2013-03-10 2:30', 'YYYY-MM-DD HH:mm').format(); //=> '2013-03-10T01:30:00-05:00'
This is because daylight saving time shifts the time from 2:00 to 3:00, so 2:30 isn't a real time. The resulting time is browser-dependent, either adjusting the time forward or backwards. Use
moment#isDSTShiftedto
test for this condition.
Note: before 2.3.0, Moment objects in this condition always returned
falsefor
moment#isValid;
they now return
true.
是否 Moment 对象 1.5.0+
编辑moment.isMoment(obj);
To check if a variable is a moment object, use
moment.isMoment().
moment.isMoment() // false moment.isMoment(new Date()) // false moment.isMoment(moment()) // true
是否 Date 对象 2.9.0+
编辑moment.isDate(obj);
To check if a variable is a native js Date object, use
moment.isDate().
moment.isDate(); // false moment.isDate(new Date()); // true moment.isDate(moment()); // false
国际化
Moment.js has robust support for internationalization.You can load multiple locales and easily switch between them.
In addition to assigning a global locale, you can assign a locale to a specific moment.
设置语言 (全局) 1.0.0+
编辑// From 2.8.1 onward moment.locale(String); moment.locale(String[]); moment.locale(String, Object); // Deprecated in 2.8.1 moment.lang(String); moment.lang(String[]); moment.lang(String, Object);
By default, Moment.js comes with English locale strings. If you need other locales, you can load them into Moment.js for later use.
To load a locale, pass the key and the string values to
moment.locale.
More details on each of the parts of the locale bundle can be found in the customization section.
moment.locale('fr', { months : "janvier_février_mars_avril_mai_juin_juillet_août_septembre_octobre_novembre_décembre".split("_"), monthsShort : "janv._févr._mars_avr._mai_juin_juil._août_sept._oct._nov._déc.".split("_"), weekdays : "dimanche_lundi_mardi_mercredi_jeudi_vendredi_samedi".split("_"), weekdaysShort : "dim._lun._mar._mer._jeu._ven._sam.".split("_"), weekdaysMin : "Di_Lu_Ma_Me_Je_Ve_Sa".split("_"), longDateFormat : { LT : "HH:mm", LTS : "HH:mm:ss", L : "DD/MM/YYYY", LL : "D MMMM YYYY", LLL : "D MMMM YYYY LT", LLLL : "dddd D MMMM YYYY LT" }, calendar : { sameDay: "[Aujourd'hui à] LT", nextDay: '[Demain à] LT', nextWeek: 'dddd [à] LT', lastDay: '[Hier à] LT', lastWeek: 'dddd [dernier à] LT', sameElse: 'L' }, relativeTime : { future : "dans %s", past : "il y a %s", s : "quelques secondes", m : "une minute", mm : "%d minutes", h : "une heure", hh : "%d heures", d : "un jour", dd : "%d jours", M : "un mois", MM : "%d mois", y : "une année", yy : "%d années" }, ordinalParse : /\d{1,2}(er|ème)/, ordinal : function (number) { return number + (number === 1 ? 'er' : 'ème'); }, meridiemParse: /PD|MD/, isPM: function (input) { return input.charAt(0) === 'M'; }, // in case the meridiem units are not separated around 12, then implement // this function (look at locale/id.js for an example) // meridiemHour : function (hour, meridiem) { // return /* 0-23 hour, given meridiem token and hour 1-12 */ // }, meridiem : function (hours, minutes, isLower) { return hours < 12 ? 'PD' : 'MD'; }, week : { dow : 1, // Monday is the first day of the week. doy : 4 // The week that contains Jan 4th is the first week of the year. } });
Once you load a locale, it becomes the active locale. To change active locales, simply call
moment.localewith the key of
a loaded locale.
moment.locale('fr'); moment(1316116057189).fromNow() // il y a une heure moment.locale('en'); moment(1316116057189).fromNow() // an hour ago
As of 2.8.0, changing the global locale doesn't affect existing instances.
moment.locale('fr'); var m = moment(1316116057189); m.fromNow(); // il y a une heure moment.locale('en'); m.fromNow(); // il y a une heure moment(1316116057189).fromNow(); // an hour ago
moment.localereturns the locale used. This is useful because Moment won't change locales if it doesn't know the one you
specify.
moment.locale('fr'); // 'fr' moment.locale('tq'); // 'fr'
You may also specify a list of locales, and Moment will use the first one it has localizations for.
moment.locale(['tq', 'fr']); // 'fr'
Moment will also try locale specifier substrings from most-specific to least-specific until it finds a locale it knows. This is useful when supplying Moment with a locale string pulled from the user's environment,
such as
window.navigator.language.
moment.locale('en-NZ'); // 'en'
Finally, Moment will search intelligently through an array of locales and their substrings.
moment.locale('en-NZ', 'en-AU'); // 'en-au', not 'en'
设置语言 (局部) 1.7.0+
编辑// From version 2.8.1 onward moment().locale(String); // Deprecated version 2.8.1 moment().lang(String);
A global locale configuration can be problematic when passing around moments that may need to be formatted into different locale.
In 1.7.0 we added instance specific locale configurations.
moment.locale('en'); // default the locale to English var localLocale = moment(); localLocale.locale('fr'); // set this instance to use French localLocale.format('LLLL'); // dimanche 15 juillet 2012 11:01 moment().format('LLLL'); // Sunday, July 15 2012 11:01 AM moment.locale('es'); // change the global locale to Spanish localLocale.format('LLLL'); // dimanche 15 juillet 2012 11:01 moment().format('LLLL'); // Domingo 15 Julio 2012 11:01 localLocale.locale(false); // reset the instance locale localLocale.format('LLLL'); // Domingo 15 Julio 2012 11:01 moment().format('LLLL'); // Domingo 15 Julio 2012 11:01
If you call
moment#localewith no parameters, you get back the locale configuration that would be used for that moment.
var fr = moment().locale('fr'); fr.localeData().months(moment([2012, 0])) // "janvier" fr.locale('en'); fr.localeData().months(moment([2012, 0])) // "January"
If you need to access the locale data for a moment, this is the preferred way to do so.
As of 2.3.0, you can also specify an array of locale identifiers. It works the same was it does in the global
locale configuration.
加载语言 (NodeJS) 1.0.0+
编辑moment.locale(String);
Loading locales in NodeJS is super easy. If there is a locale file in
moment-root/locale/named after that key, the first
call to
moment.localewill load it.
var moment = require('moment'); moment.locale('fr'); moment(1316116057189).fromNow(); // il y a une heure
If you want your locale supported, create a pull request to the
developbranch with the required
locale and unit test files.
加载语言 (浏览器) 1.0.0+
编辑// From 2.8.1 onward moment.locale(String, Object); // Deprecated in 2.8.1 moment.lang(String, Object);
Loading locales in the browser just requires you to include the locale files.
<script src="moment.js"></script> <script src="locale/fr.js"></script> <script src="locale/pt.js"></script> <script> moment.locale('fr'); // Set the default/global locale // ... </script>
There are minified versions of all locales together:
<script src="moment.js"></script> <script src="min/locales.js"></script>
To minimize http requests, use our Grunt task to compile Moment with a custom list of locales:
grunt transpile:fr,it
<script src="min/moment-with-locales.custom.js"></script>
Note: Locale files are defined in UMD style, so they should work seamlessly in all environments.
新增语言
编辑To add your locale to Moment.js, submit a pull request with both a locale file and a test file. You can find examples in
moment/locale/fr.jsand
moment/test/locale/fr.js.
To run the tests in Node.js, do
npm install, then
grunt.
If all the tests pass, submit a pull request, and thank you for contributing!
获取当前语言 1.6.0+
编辑// From version 2.8.1 onward moment.locale(); // Deprecated in version 2.8.1 moment.lang();
If you are changing locales frequently, you may want to know what locale is currently being used. This is as simple as calling
moment.localewithout
any parameters.
moment.locale('en'); // set to english moment.locale(); // returns 'en' moment.locale('fr'); // set to french moment.locale(); // returns 'fr'
列出月份和星期 2.3.0+
编辑moment.months() moment.monthsShort() moment.weekdays() moment.weekdaysShort() moment.weekdaysMin()
It is sometimes useful to get the list of months or weekdays in a locale, for example when populating a dropdown menu.
moment.months();
Returns the list of months in the current locale.
[ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]
Similarly,
moment.monthsShortreturns abbreviated month names, and
moment.weekdays,
moment.weekdaysShort,
moment.weekdaysMinreturn
lists of weekdays.
You can pass an integer into each of those functions to get a specific month or weekday.
moment.weekdays(3); // 'Wednesday'
Note: Currently, weekdays always have Sunday as index 0, regardless of the local first day of the week.
Some locales make special considerations into account when formatting month names. For example, Dutch formats month abbreviations without a trailing period, but only if it's formatting the month between dashes.
The
monthsmethod supports passing a format in so that the months will be listed in the proper context.
moment.locale('nl'); moment.monthsShort(); // ['jan.', 'feb.', 'mrt.', ...] moment.monthsShort('-MMM-'); // [ 'jan', 'feb', 'mrt', ...]
And finally, you can combine both the format option and the integer option.
moment.monthsShort('-MMM-', 3); // 'apr'
获取语言数据 2.8.0+
编辑localeData = moment.localeData() localeData.months() localeData.monthsShort() localeData.monthsParse() localeData.weekdays() localeData.weekdaysShort() localeData.weekdaysMin() localeData.weekdaysParse() localeData.longDateFormat() localeData.isPM() localeData.meridiem() localeData.calendar() localeData.relativeTime() localeData.pastFuture() localeData.ordinal() localeData.preparse() localeData.postformat() localeData.weeks() localeData.invalidDate() localeData.firstDayOfWeek() localeData.firstDayOfYear()
You can access the properties of the currently loaded locale through the
moment.localeData(key)function. It returns the
current locale or a locale with the given key:
// get current locale var currentLocaleData = moment.localeData(); var frLocaleData = moment.localeData('fr');
The returned object has the following methods:
localeData.months(aMoment); // full month name of aMoment localeData.monthsShort(aMoment); // short month name of aMoment localeData.monthsParse(longOrShortMonthString); // returns month id (0 to 11) of input localeData.weekdays(aMoment); // full weekday name of aMoment localeData.weekdaysShort(aMoment); // short weekday name of aMoment localeData.weekdaysMin(aMoment); // min weekday name of aMoment localeData.weekdaysParse(minShortOrLongWeekdayString); // returns weekday id (0 to 6) of input localeData.longDateFormat(dateFormat); // returns the full format of abbreviated date-time formats LT, L, LL and so on localeData.isPM(amPmString); // returns true iff amPmString represents PM localeData.meridiem(hours, minutes, isLower); // returns am/pm string for particular time-of-day in upper/lower case localeData.calendar(key, aMoment); // returns a format that would be used for calendar representation. Key is one of 'sameDay', 'nextDay', 'lastDay', 'nextWeek', 'prevWeek', 'sameElse' localeData.relativeTime(number, withoutSuffix, key, isFuture); // returns relative time string, key is on of 's', 'm', 'mm', 'h', 'hh', 'd', 'dd', 'M', 'MM', 'y', 'yy'. Single letter when number is 1. localeData.pastFuture(diff, relTime); // convert relTime string to past or future string depending on diff localeData.ordinal(number); // convert number to ordinal string 1 -> 1st localeData.preparse(str); // called before parsing on every input string localeData.postformat(str); // called after formatting on every string localeData.week(aMoment); // returns week-of-year of aMoment localeData.invalidDate(); // returns a translation of 'Invalid date' localeData.firstDayOfWeek(); // 0-6 (Sunday to Saturday) localeData.firstDayOfYear(); // 0-15 this and the first day of week are used // to determine which is the first week of the // year. dow == 1 and doy == 4 means week starts // Monday and first week that has Thursday is the // first week of the year (but doy is NOT simply // Thursday).
自定义
Moment.js is very easy to customize. In general, you should create a locale setting with your customizations.moment.locale('en-my-settings', { // customizations. });
However, you can also overwrite an existing locale that has been loaded as well.
moment.locale('en', { // customizations });
Any settings that are not defined are inherited from the default english settings.
You can remove a previously defined locale by passing
nullas the second argument. The deleted locale will no longer be
available for use.
moment.locale('fr'); // 'fr' moment.locale('en'); // 'en' moment.locale('fr', null); moment.locale('fr'); // 'en'
月份名 1.0.0+
编辑// From 2.8.1 onward moment.locale('en', { months : String[] }); moment.locale('en', { months : Function }); // Deprecated in 2.8.1 moment.lang('en', { months : String[] }); moment.lang('en', { months : Function });
Locale#monthsshould be an array of the month names.
moment.locale('en', { months : [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ] });
If you need more processing to calculate the name of the month, (for example, if there is different grammar for different formats),
Locale#monthscan
be a function with the following signature. It should always return a month name.
moment.locale('en', { months : function (momentToFormat, format) { // momentToFormat is the moment currently being formatted // format is the formatting string if (/^MMMM/.test(format)) { // if the format starts with 'MMMM' return nominative[momentToFormat.month()]; } else { return subjective[momentToFormat.month()]; } } });
月份名 (缩写) 1.0.0+
编辑// From 2.8.1 onward moment.locale('en', { monthsShort : String[] }); moment.locale('en', { monthsShort : Function }); // Deprecated in 2.8.1 moment.lang('en', { monthsShort : String[] }); moment.lang('en', { monthsShort : Function });
Locale#monthsShortshould be an array of the month abbreviations.
moment.locale('en', { monthsShort : [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ] });
Like
Locale#months,
Locale#monthsShortcan
be a callback function as well.
moment.locale('en', { monthsShort : function (momentToFormat, format) { if (/^MMMM/.test(format)) { return nominative[momentToFormat.month()]; } else { return subjective[momentToFormat.month()]; } } });
星期名 1.0.0+
编辑// From version 2.8.1 onward moment.locale('en', { weekdays : String[] }); moment.locale('en', { weekdays : Function }); // Deprecated version 2.8.1 moment.lang('en', { weekdays : String[] }); moment.lang('en', { weekdays : Function });
Locale#weekdaysshould be an array of the weekdays names.
moment.locale('en', { weekdays : [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ] });
Locale#weekdayscan be a callback function as well.
moment.locale('en', { weekdays : function (momentToFormat, format) { return weekdays[momentToFormat.day()]; } });
星期名 (缩写) 1.0.0+
编辑// From 2.8.1 onward moment.locale('en', { weekdaysShort : String[] }); moment.locale('en', { weekdaysShort : Function }); // Deprecated in 2.8.1 moment.lang('en', { weekdaysShort : String[] }); moment.lang('en', { weekdaysShort : Function });
Locale#weekdaysShortshould be an array of the weekdays abbreviations.
moment.locale('en', { weekdaysShort : ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] });
Locale#weekdaysShortcan be a callback function as well.
moment.locale('en', { weekdaysShort : function (momentToFormat, format) { return weekdaysShort[momentToFormat.day()]; } });
星期名 (极简缩写) 1.7.0+
编辑// From 2.8.1 onward moment.locale('en', { weekdaysMin : String[] }); moment.locale('en', { weekdaysMin : Function }); // Deprecated in 2.8.1 moment.lang('en', { weekdaysMin : String[] }); moment.lang('en', { weekdaysMin : Function });
Locale#weekdaysMinshould be an array of two letter weekday abbreviations. The purpose of these is for things like calendar
pickers, thus they should be as small as possible.
moment.locale('en', { weekdaysMin : ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"] });
Locale#weekdaysMincan be a callback function as well.
moment.locale('en', { weekdaysMin : function (momentToFormat, format) { return weekdaysMin[momentToFormat.day()]; } });
长日期格式 1.1.0+
编辑// From 2.8.1 onward moment.locale('en', { longDateFormat : Object }); // Deprecated in 2.8.1 moment.lang('en', { longDateFormat : Object });
Locale#longDateFormatshould be an object containing a key/value pair for each long date format
L LL LLL LLLL LT LTS.
LTshould be the time format, and is also used for
moment#calendar.
moment.locale('en', { longDateFormat : { LT: "h:mm A", LTS: "h:mm:ss A", L: "MM/DD/YYYY", l: "M/D/YYYY", LL: "MMMM Do YYYY", ll: "MMM D YYYY", LLL: "MMMM Do YYYY LT", lll: "MMM D YYYY LT", LLLL: "dddd, MMMM Do YYYY LT", llll: "ddd, MMM D YYYY LT" } });
You can eliminate the lowercase
ltokens and they will be created automatically by replacing long tokens with the short
token variants.
moment.locale('en', { longDateFormat : { LT: "h:mm A", LTS: "h:mm:ss A", L: "MM/DD/YYYY", LL: "MMMM Do YYYY", LLL: "MMMM Do YYYY LT", LLLL: "dddd, MMMM Do YYYY LT" } });
相对时间 1.0.0+
编辑// From 2.8.1 onward moment.locale('en', { relativeTime : Object }); // Deprecated in 2.8.1 moment.lang('en', { relativeTime : Object });
Locale#relativeTimeshould be an object of the replacement strings for
moment#from.
moment.locale('en', { relativeTime : { future: "in %s", past: "%s ago", s: "seconds", m: "a minute", mm: "%d minutes", h: "an hour", hh: "%d hours", d: "a day", dd: "%d days", M: "a month", MM: "%d months", y: "a year", yy: "%d years" } });
Locale#relativeTime.futurerefers to the prefix/suffix for future dates, and
Locale#relativeTime.pastrefers
to the prefix/suffix for past dates. For all others, a single character refers to the singular, and a double character refers to the plural.
If a locale requires additional processing for a token, it can set the token as a function with the following signature. The function should return a string.
function (number, withoutSuffix, key, isFuture) { return string; }
The
keyargument refers to the replacement key in the
Locale#relativeTimeobject.
(eg.
s m mm h, etc.)
The
numberargument refers to the number of units for that key. For
m,
the number is the number of minutes, etc.
The
withoutSuffixargument will be true if the token will be displayed without a suffix, and false if it will be displayed
with a suffix. (The reason for the inverted logic is because the default behavior is to display with the suffix.)
The
isFutureargument will be true if it is going to use the future suffix/prefix and false if it is going to use the past
prefix/suffix. The
isFutureargument was added in version 1.6.0.
上午/下午 1.6.0+
编辑// From 2.8.1 onward moment.locale('en', { meridiem : Function }); // Deprecated in 2.8.1 moment.lang('en', { meridiem : Function });
If your locale uses 'am/pm',
Locale#meridiemcan be omitted, as those values are the defaults.
If your locale needs any different computation for am/pm,
Locale#meridiemshould be a callback function that returns the
correct string based on hour, minute, and upper/lowercase.
moment.locale('zh-cn', { meridiem : function (hour, minute, isLowercase) { if (hour < 9) { return "早上"; } else if (hour < 11 && minute < 30) { return "上午"; } else if (hour < 13 && minute < 30) { return "中午"; } else if (hour < 18) { return "下午"; } else { return "晚上"; } } });
Before version 1.6.0,
Locale#meridiemwas a map of upper and lowercase versions of am/pm.
moment.locale('en', { meridiem : { am : 'am', AM : 'AM', pm : 'pm', PM : 'PM' } });
This has been deprecated. The 1.6.0 callback function syntax is now used instead.
上午/下午 (解析) 2.1.0+
编辑// From 2.8.1 onward moment.locale('en', { meridiemParse : RegExp isPM : Function }); // Deprecated in 2.8.1 moment.lang('en', { meridiemParse : RegExp isPM : Function });
Locale#isPMshould return true if the input string is past 12 noon. This is used in parsing the
a Atokens.
moment.locale('en', { isPM : function (input) { return ((input + '').toLowerCase()[0] === 'p'); } });
To configure what strings should be parsed as input, set the
meridiemParseproperty.
moment.locale('en', { meridiemParse : /[ap]\.?m?\.?/i });
日历 1.3.0+
编辑// From 2.8.1 onward moment.locale('en', { calendar : Object }); // Deprecated in 2.8.1 moment.lang('en', { calendar : Object });
Locale#calendarshould have the following formatting strings.
moment.locale('en', { calendar : { lastDay : '[Yesterday at] LT', sameDay : '[Today at] LT', nextDay : '[Tomorrow at] LT', lastWeek : '[last] dddd [at] LT', nextWeek : 'dddd [at] LT', sameElse : 'L' } });
Each of the
Locale#calendarkeys can also be a callback function with the scope of the current moment. It should return
a formatting string.
function () { return '[hoy a la' + ((this.hours() !== 1) ? 's' : '') + '] LT'; },
比较 1.0.0+
编辑// From 2.8.1 onward moment.locale('en', { ordinal : Function }); // Deprecated in 2.8.1 moment.lang('en', { ordinal : Function });
Locale#ordinalshould be a function that returns the ordinal for a given number.
moment.locale('en', { ordinal : function (number, token) { var b = number % 10; var output = (~~ (number % 100 / 10) === 1) ? 'th' : (b === 1) ? 'st' : (b === 2) ? 'nd' : (b === 3) ? 'rd' : 'th'; return number + output; } });
As of 2.0.0, the ordinal function should return both the number and the ordinal. Previously, only the ordinal was returned.
As of 2.1.0, the token parameter was added. It is a string of the token that is being ordinalized, for example:
Mor
d.
For more information on ordinal numbers, see wikipedia
相对时间临界值 2.7.0+
编辑moment.relativeTimeThreshold(unit); // getter moment.relativeTimeThreshold(unit, limit); // setter
duration.humanizehas thresholds which define when a unit is considered a minute, an hour and so on. For example, by default
more than 45 seconds is considered a minute, more than 22 hours is considered a day and so on. To change those cutoffs use
moment.relativeTimeThreshold(unit, limit)where
limit is one of
s,
m,
h,
d,
M.
unit | meaning | usage |
---|---|---|
s | seconds | least number of seconds to be considered a minute |
m | minutes | least number of minutes to be considered an hour |
h | hours | least number of hours to be considered a day |
d | days | least number of days to be considered a month |
M | months | least number of months to be considered a year |
// Retrieve existing thresholds moment.relativeTimeThreshold('s'); // 45 moment.relativeTimeThreshold('m'); // 45 moment.relativeTimeThreshold('h'); // 22 moment.relativeTimeThreshold('d'); // 26 moment.relativeTimeThreshold('M'); // 11 // Set new thresholds moment.relativeTimeThreshold('s', 40); moment.relativeTimeThreshold('m', 40); moment.relativeTimeThreshold('h', 20); moment.relativeTimeThreshold('d', 25); moment.relativeTimeThreshold('M', 10);
NOTE: Retrieving thresholds was added in 2.8.1.
时间段
Moment.js also has duration objects. Where a moment is defined as single points in time, durations are defined as a length of time.Durations do not have a defined beginning and end date. They are contextless.
A duration is conceptually more similar to '2 hours' than to 'between 2 and 4 pm today'. As such, they are not a good solution to converting between units that depend on context.
For example, a year can be defined as 366 days, 365 days, 365.25 days, 12 months, or 52 weeks. Trying to convert years to days makes no sense without context. It is much better to use
moment#difffor
calculating days or years between two moments than to use
Durations.
创建 1.6.0+
编辑moment.duration(Number, String); moment.duration(Number); moment.duration(Object); moment.duration(String);
To create a duration, call
moment.duration()with the length of time in milliseconds.
moment.duration(100); // 100 milliseconds
If you want to create a moment with a unit of measurement other than milliseconds, you can pass the unit of measurement as well.
moment.duration(2, 'seconds'); moment.duration(2, 'minutes'); moment.duration(2, 'hours'); moment.duration(2, 'days'); moment.duration(2, 'weeks'); moment.duration(2, 'months'); moment.duration(2, 'years');
The same shorthand for
moment#addand
moment#subtractworks
here as well.
Key | Shorthand |
---|---|
years | y |
months | M |
weeks | w |
days | d |
hours | h |
minutes | m |
seconds | s |
milliseconds | ms |
moment#add, you can pass an object of values if you need multiple different units of measurement.
moment.duration({ seconds: 2, minutes: 2, hours: 2, days: 2, weeks: 2, months: 2, years: 2 });
As of 2.1.0, moment supports parsing ASP.NET style time spans. The following formats are supported.
The format is an hour, minute, second string separated by colons like
23:59:59. The number of days can be prefixed with
a dot separator like so
7.23:59:59. Partial seconds are supported as well
23:59:59.999.
moment.duration('23:59:59'); moment.duration('23:59:59.999'); moment.duration('7.23:59:59.999'); moment.duration('23:59'); //added in 2.3.0
As of 2.3.0, moment also supports parsing ISO 8601 durations.
moment.duration('P1Y2M3DT4H5M6S'); moment.duration('P1M');
拟人化 1.6.0+
编辑moment.duration().humanize();
Sometimes, you want all the goodness of
moment#frombut you don't want to have to create two moments, you just want to
display a length of time.
Enter
moment.duration().humanize().
moment.duration(1, "minutes").humanize(); // a minute moment.duration(2, "minutes").humanize(); // 2 minutes moment.duration(24, "hours").humanize(); // a day
By default, the return string is suffixless. If you want a suffix, pass in true as seen below.
moment.duration(1, "minutes").humanize(true); // in a minute
For suffixes before now, pass in a negative number.
moment.duration(-1, "minutes").humanize(true); // a minute ago
毫秒 1.6.0+
编辑moment.duration().milliseconds(); moment.duration().asMilliseconds();
To get the number of milliseconds in a duration, use
moment.duration().milliseconds().
It will return a number between 0 and 999.
moment.duration(500).milliseconds(); // 500 moment.duration(1500).milliseconds(); // 500 moment.duration(15000).milliseconds(); // 0
If you want the length of the duration in milliseconds, use
moment.duration().asMilliseconds()instead.
moment.duration(500).asMilliseconds(); // 500 moment.duration(1500).asMilliseconds(); // 1500 moment.duration(15000).asMilliseconds(); // 15000
秒 1.6.0+
编辑moment.duration().seconds(); moment.duration().asSeconds();
To get the number of seconds in a duration, use
moment.duration().seconds().
It will return a number between 0 and 59.
moment.duration(500).seconds(); // 0 moment.duration(1500).seconds(); // 1 moment.duration(15000).seconds(); // 15
If you want the length of the duration in seconds, use
moment.duration().asSeconds()instead.
moment.duration(500).asSeconds(); // 0.5 moment.duration(1500).asSeconds(); // 1.5 moment.duration(15000).asSeconds(); // 15
分钟 1.6.0+
编辑moment.duration().minutes(); moment.duration().asMinutes();
As with the other getters for durations,
moment.duration().minutes()gets the minutes (0 - 59).
moment.duration().asMinutes()gets the length of the duration in minutes.
小时 1.6.0+
编辑moment.duration().hours(); moment.duration().asHours();
As with the other getters for durations,
moment.duration().hours()gets the hours (0 - 23).
moment.duration().asHours()gets the length of the duration in hours.
天 1.6.0+
编辑moment.duration().days(); moment.duration().asDays();
As with the other getters for durations,
moment.duration().days()gets the days (0 - 29).
moment.duration().asDays()gets the length of the duration in days.
月 1.6.0+
编辑moment.duration().months(); moment.duration().asMonths();
As with the other getters for durations,
moment.duration().months()gets the months (0 - 11).
moment.duration().asMonths()gets the length of the duration in months.
Note: The length of a duration in months is defined as 30 days.
年 1.6.0+
编辑moment.duration().years(); moment.duration().asYears();
As with the other getters for durations,
moment.duration().years()gets the years.
moment.duration().asYears()gets the length of the duration in years.
Note: The length of a duration in years is defined as 365 days.
加法 2.1.0+
编辑moment.duration().add(Number, String); moment.duration().add(Number); moment.duration().add(Duration); moment.duration().add(Object);
Mutates the original duration by adding time.
The same keys and shorthands used to create durations can be used here as the second argument.
var a = moment.duration(1, 'd'); var b = moment.duration(2, 'd'); a.add(b).days(); // 3
减法 2.1.0+
编辑moment.duration().subtract(Number, String); moment.duration().subtract(Number); moment.duration().subtract(Duration); moment.duration().subtract(Object);
Mutates the original duration by subtracting time.
The same keys and shorthands used to create durations can be used here as the second argument.
var a = moment.duration(3, 'd'); var b = moment.duration(2, 'd'); a.subtract(b).days(); // 1
转换单位 2.1.0+
编辑moment.duration().as(String);
As an alternate to
Duration#asX, you can use
Duration#as('x').
All the shorthand keys from
moment#addapply here
as well.
duration.as('hours'); duration.as('minutes'); duration.as('seconds'); duration.as('milliseconds');
取值 2.1.0+
编辑moment.duration().get(String);
As an alternate to
Duration#x()getters, you can use
Duration#get('x').
All the shorthand keys from
moment#addapply here
as well.
duration.get('hours'); duration.get('minutes'); duration.get('seconds'); duration.get('milliseconds');
输出 JSON 2.9.0+
编辑moment.duration().toJSON();
When serializing a duration object to JSON, it will be be represented as an ISO8601 string.
JSON.stringify({ postDuration : moment.duration(5, 'm') }); // '{"postDuration":"PT5M"}'
Is a Duration 1.6.0+
编辑moment.isDuration(obj);
To check if a variable is a moment duration object, use
moment.isDuration().
moment.isDuration() // false moment.isDuration(new Date()) // false moment.isDuration(moment()) // false moment.isDuration(moment.duration()) // true moment.isDuration(moment.duration(2, 'minutes')) // true
工具类函数
Moment exposes some methods which may be useful to people extending the library or writing custom parsers.
标准化单位 2.3.0+
编辑moment.normalizeUnits(String);
Many of Moment's functions allow the caller to pass in aliases for unit enums. For example, all of the
gets below are equivalent.
var m = moment(); m.get('y'); m.get('year'); m.get('years');
If you're extending the library, you may want access to Moment's facilities for that in order to better align your functionality with Moment's.
moment.normalizeUnits('y'); // 'year' moment.normalizeUnits('Y'); // 'year' moment.normalizeUnits('year'); // 'year' moment.normalizeUnits('years'); // 'year' moment.normalizeUnits('YeARS'); // 'year'
无效对象 2.3.0+
编辑moment.invalid(Object);
You can create your own invalid Moment objects, which is useful in making your own parser.
var m = moment.invalid(); m.isValid(); // false m.format(); // 'Invalid date' m.parsingFlags().userInvalidated; // true
invalidalso accepts an object which specifies which parsing flags to set. This will not set the
userInvalidatedparsing
flag unless it's one of the properties specified.
var m = moment.invalid({invalidMonth: 'Actober'}); m.parsingFlags().invalidMonth; // 'Actober'
You need not specify parsing flags recognized by Moment; the Moment will be invalid nonetheless, and the parsing flags will be returned by
parsingFlags().
插件
Some other people have made plugins for Moment.js that may be useful to you.
Strftime
编辑npm install moment-strftime
If you are more comfortable working with strftime instead of LDML-like parsing tokens, you can use Ben Oakes' plugin
moment-strftime.
The repository is located at github.com/benjaminoakes/moment-strftime
ISO Calendar
编辑npm install moment-isocalendar
If you are looking for a Python-like isocalendar method, you can use Rocky Meza's plugin
moment-isocalendar
Calling the isocalendar method on a moment will return an array like the following:
[year, week_of_year, day_of_week, minutes_since_midnight]
moment().isocalendar(); // [2012, 8, 5, 870]
You can also reconstruct a moment from a isocalendar array.
moment.fromIsocalendar([2011, 51, 5, 870]).format('LLLL'); // "Friday, December 23 2011 2:30 PM"
The repository is located at github.com/fusionbox/moment-isocalendar
Date Ranges
编辑npm install moment-range
If you need to work with date ranges, you can use Gianni Chiappetta's plugin
moment-range.
Documentation can be found on the homepage github.com/gf3/moment-range.
And it is also available for the web at the repository below.
The repository is located at github.com/gf3/moment-range
Twix
编辑npm install twix
Another range plugin is Isaac Cambron's library
Twix. It has many range-related features and excels at formatting ranges
readably. For example,
var t = moment("1/25/1982 9:30 AM").twix("1/25/1982 1:30 PM"); t.isCurrent(); // false t.count('minutes'); // 241 t.format(); // 'Jan 25, 1982, 9:30 AM - 1:30 PM' t.simpleFormat("h:m"); // '9:30 - 1:30'
Full documentation of all the options and features is here.
It's available on npm like so:
npm install twix
Or just grab the JS file from here.
Twitter
编辑If you're trying to format times for tweets like the way Twitter does, you can use the moment.twitter plugin
by @hijonathan.
It's a simple way to display both short and long versions of human-readable timestamps.
moment().subtract(5, 'hours').twitterLong(); // 5 hours
Yes, it does smart pluralization.
moment().subtract(1, 'hour').twitterLong(); // 1 hour
Not short enough for you?
moment().subtract(6, 'days').twitterShort(); // 6d
Jalaali Calendar
编辑npm install moment-jalaali
If you want to work with Jalaali calendar system (Jalali, Persian, Khorshidi or Shamsi), you can use Behrang Noruzi Niya's plugin
moment-jalaali.
When installed, it will wrap
momentand moment will be able to format and parse Jalaali years and months. Here is a short
example:
var m = moment('1360/5/26', 'jYYYY/jM/jD'); // Parse a Jalaali date. m.format('jYYYY/jM/jD [is] YYYY/M/D'); // 1360/5/26 is 1981/8/17
The repository is located at github.com/behrang/moment-jalaali.
MSDate
编辑If you are using OLE Automation dates in .NET check out Markit On Demand's
moment-msdate. Using this plugin allows you
to format OA dates into JavaScript dates and vice-versa.
Convert a
momentto an OA date:
moment().toOADate(); // a floating point number
Or, convert an OA date to a
moment:
moment.fromOADate(41493); // Wed Aug 07 2013 00:00:00 GMT-0600 (MDT)
More information and detailed docs can be found on GitHub at http://markitondemand.github.io/moment-msdate/.
Fiscal Quarters
编辑If you ever have need for Fiscal, Calendar or Academic quarters, you can use the moment-fquarter plugin
by @robgallen.
At its simplest, just call the fquarter method on any moment object. It returns a formatted string with April being the first quarter.
moment("2013-01-01").fquarter(); // Q4 2012/13
You can pass in any month as the starting quarter, e.g. July
moment("2013-01-01").fquarter(7); // Q3 2012/13
If you want calendar quarters, start in January
moment("2013-01-01").fquarter(1); // Q1 2013
Precise Range
编辑The Precise Range plugin, written by Rob
Dawson, can be used to display exact, human-readable representations of date/time ranges
moment("2014-01-01 12:00:00").preciseDiff("2015-03-04 16:05:06"); // 1 year 2 months 3 days 4 hours 5 minutes 6 seconds
moment.preciseDiff("2014-01-01 12:00:00", "2014-04-20 12:00:00"); // 3 months 19 days
Recur
编辑npm install moment-recur
If you need to work with recurring dates, you can use Casey Trimm's plugin
moment-recur.
This plugin will allow you to create length-based intervals (days, weeks, etc.) and calendar-based intervals (daysOfMonth, monthsOfYear, etc.).
It provides a
matchesfunction to test whether a date recurs according to the rules set, as well as generator functions
to get the next and previous dates in a series.
The repository, documentation, and many more examples can be found at github.com/c-trimm/moment-recur
var interval = moment( "01/01/2014" ).recur().every(2).days(); // Length Interval interval.matches( "01/03/2014" ); // true interval.next( 2, "L" ); // ["01/03/2014", "01/05/2014"] interval.forget( "days" ); // Remove a rule interval.dayOfMonth( 10 ); // Calendar Interval interval.matches( "05/10/2014" ); // true interval.previous( 2, "L" ); // ["12/10/2013", "11/10/2013"]
Parse Date Format
编辑npm install moment-parseformat
This plugin extracts the format of a date/time string.
var format = moment.parseFormat('Thursday, February 6th, 2014 9:20pm'); // dddd, MMMM Do, YYYY h:mma moment().format(format); // format
That allows to create smart date inputs that let your users set a Date/Time and lets you extract the user's preferred format for future usage. Find an example usage of it at minutes.io.
The Plugin has been authored by @gr2m. Links: Demo | Source
Java DateFormat Parser
编辑npm install moment-jdateformatparser
If you want to work with the
java.text.DateFormatyou can use this plugin.
For example,
moment("2013-12-24 14:30").formatWithJDF("dd.MM.yyyy"); // returns the formatted date "24.12.2013" moment().toJDFString("DD.MM.YYYY"); // returns the Java format pattern "dd.MM.yyyy"
The repository is located at github.com/MadMG/moment-jdateformatparser
Hijri Calendar
编辑npm install moment-hijri
If you want to work with Hijri calendar then you can use
moment-hijriplugin.
moment-hijriis
a moment plugin for the Hijri lunar calendar based on Umm al-Qura calculations. This plugin is developed by Suhail
Alkowaileet.
When you install it, it will wrap
momentand you will be able to parse Hijri dates. Here is a short example:
m = moment('1410/8/28', 'iYYYY/iM/iD'); // Parse a Hijri date. m.format('iYYYY/iM/iD [is] YYYY/M/D'); // 1410/8/28 is 1990/3/25
The repository is located at github.com/xsoh/moment-hijri.
Transform
编辑bower install moment-transform
moment-transformis
a plugin that manipulated dates through patterns. You can use basic operations –set/add/substract– on individual parts (hours, month, …) of a Moment instance.
moment().transform('YYYY-MM-+01 00:00:00.000'); // Tonight at midnight moment().transform('14:30:00.000'); // Today, 2:30 pm moment().transform('YYYY-MM--30 00:00:00.000'); // 30 days ago
Optional parameters lets you specify custom patterns and force strict pattern usage (non-alphabetic characters are not mandatory in passed string by default).
moment().transform('+01MMYYYY', 'DD/MM/YYYY', false); // Tomorrow, same time moment().transform('+01MMYYYY', 'DD/MM/YYYY', true); // Invalid date
更多有关moment.js的介绍,请访问项目官网:http://momentjs.com/
当前时间:moment().format('YYYY-MM-DD HH:mm:ss');
2017-09-07 10:36:14
今天是星期几:moment().format('d');
4
Unix时间戳:moment().format('X');1504751774
相对时间:moment("20130101", "YYYYMMDD").fromNow();
5 years ago
相对时间:moment("20151010", "YYYYMMDD").fromNow();
2 years ago
7天后的日期:moment().add('days',7).format('YYYY年MM月DD日');
2017年09月14日
9小时后:moment().add('hours',9).format('HH:mm:ss');19:36:14
明天moment().add(1, 'days').calendar();
Tomorrow at 10:36 AM
相关文章推荐
- 使用moment.js轻松管理日期和时间
- 使用moment.js轻松管理日期和时间
- 使用moment.js轻松管理日期和时间
- [转] 使用moment.js轻松管理日期和时间
- 使用moment.js轻松管理日期和时间
- 使用moment.js轻松管理日期和时间
- 使用moment.js轻松管理日期和时间
- 使用moment.js轻松管理日期和时间
- 使用moment.js轻松管理日期和时间
- moment.js 日期和时间管理 24小时制
- 时间处理工具 moment.js:管理时间和日期,so easy!
- 【Flask】 使用Flask-Moment进行日期时间的管理
- moment.js 日期和时间管理 24小时制
- 使用moment.js管理时间
- moment.js 日期和时间管理 24小时制
- 使用Swing的JSpinner组件设置日期时间选择器
- 与moment.js整合使用的javascript日期选择期 - Pikaday
- js中使用eval()方法将字符串转换成日期格式、并获取指定时间的日期
- 详解Angularjs在控制器(controller.js)中使用过滤器($filter)格式化日期/时间实例
- Python Flask Web 第七课 —— 使用 flask-moment 本地化日期和时间