moment(String) 带格式

moment(String) 带格式

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

Tokens are case-sensitive.

InputExampleDescription

YYYY20144 or 2 digit year

YY142 digit year

Y-25Year with any number of digits and sign

Q1..4Quarter of year. Sets month to first month in quarter.

M MM1..12Month number

MMM MMMMJan..DecemberMonth name in locale set by moment.locale()

D DD1..31Day of month

Do1st..31stDay of month with ordinal

DDD DDDD1..365Day of year

X1410715640.579Unix timestamp

x1410715640579Unix ms timestamp

YYYY from version 2.10.5 supports 2 digit years, and converts them to a year

near 2000 (same as YY).

Y was added in 2.11.1. It will match any number, signed or unsigned. It is useful for years that are not 4 digits or are before the common era. It can be used for any year.

Week year, week, and weekday tokens

For these, the lowercase tokens use the locale aware week start days, and the uppercase tokens use the ISO week date start days.

Tokens are case-sensitive.

InputExampleDescription

gggg2014Locale 4 digit week year

gg14Locale 2 digit week year

w ww1..53Locale week of year

e0..6Locale day of week

ddd ddddMon...SundayDay name in locale set by moment.locale()

GGGG2014ISO 4 digit week year

GG14ISO 2 digit week year

W WW1..53ISO week of year

E1..7ISO day of week

Locale aware formats

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 LTS which was added

2.8.4.

Tokens are case-sensitive.

InputExampleDescription

L04/09/1986Date (in local format)

LLSeptember 4 1986Month name, day of month, year

LLLSeptember 4 1986 8:30 PMMonth name, day of month, year, time

LLLLThursday, September 4 1986 8:30 PMDay of week, month name, day of month, year, time

LT08:30 PMTime (without seconds)

LTS08:30:00 PMTime (with seconds)

Hour, minute, second, millisecond, and offset tokens

Tokens are case-sensitive.

InputExampleDescription

H HH0..23Hours (24 hour time)

h hh1..12Hours (12 hour time used with a A.)

k kk1..24Hours (24 hour time from 1 to 24)

a Aam pmPost or ante meridiem (Note the one character a p are also considered valid)

m mm0..59Minutes

s ss0..59Seconds

S SS SSS0..999Fractional seconds

Z ZZ+12:00Offset from UTC as +-HH:mm, +-HHmm, or Z

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.

Note that the number of S characters provided is only relevant when parsing in strict mode.

In standard mode, S, SS, SSS, SSSS are all equivalent, and interpreted as fractions of a second.

For example, .12 is always 120 milliseconds, passing SS will not cause it to be interpreted as 12 milliseconds.

Z ZZ were added in version 1.2.0.

S SS SSS were added in version 1.6.0.

X was added in version 2.0.0.

k kk was added in version 2.18.0

Unless you specify a time zone offset, parsing a string will create a date in the current time zone.

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#isValid will 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/unexpected behavior.

For example, the following behavior can be observed:

moment('2016 is a date', 'YYYY-MM-DD').isValid() //true, 2016 was matched

Previous to 2.13.0 the parser exhibited the following behavior. This has been corrected.

moment('I am spartacus', 'h:hh A').isValid(); //true - the 'am' matches the 'A' flag.

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, including delimeters.

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

moment('2012.05.25', 'YYYY-MM-DD', true).isValid(); // false

You can use both locale and strictness.

moment('2012-10-14', 'YYYY-MM-DD', 'fr', true);

Strict parsing is frequently the best parsing option. For more information about choosing strict vs forgiving parsing, see the parsing guide.

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.parseTwoDigitYear method. The only argument of this method is a string containing the two years input by the user, and should return the year as an integer.

moment.parseTwoDigitYear = function(yearString) {

return parseInt(yearString) + 2000;

}

Parsing glued hour and minutes

From version 2.11.0 parsing hmm, Hmm, hmmss and Hmmss is supported:

moment("123", "hmm").format("HH:mm") === "01:23"

moment("1234", "hmm").format("HH:mm") === "12:34"

相关推荐

滑板装备用到什么程度才需要换?土豪勿进!
【噬】的甲骨文金文篆文字形演变含义日期:2023-03-19 来源:甲骨密码评论:0点击:
gtx770显卡价格
网上365bet

gtx770显卡价格

📅 07-06 👁️ 2148
word打开是只读模式,怎么回事?
365彩票软件app下载

word打开是只读模式,怎么回事?

📅 01-03 👁️ 4223
淘宝店铺付款后多久到账?付款后淘宝商家多久之内发货?
北京朝阳区全棉时代网点分布
365彩票软件app下载

北京朝阳区全棉时代网点分布

📅 11-03 👁️ 374