Moment.js Date wrapper class Moment.js1 Create Time Object
Moment (); Current time
Moment (new Date (2020, 3, 7)); Date Object creation
Moment (1318781876406); Time Stamp Creation
Moment ("12-25-1995", "mm-dd-yyyy"); Date text + date format
Moment ("12-25-1995", ["mm-dd-yyyy", "YYYY-MM-DD"]);
Moment ([year, month, date, hours, minutes, seconds, milliseconds]); Array creation
Moment.unix (number) Unix time
Date format table
Input |
Output |
M or MM |
Month Number (1-12) |
MMM or MMMM |
Month Name (in currently language set by ' Moment.lang () ') |
D or DD |
Day of month |
DDD or DDDD |
Day of the Year |
D, DD, DDD, or dddd |
Day of week (Note:these tokens is used to create the date, as there is 4-5 weeks in a month, and it would be imposs Ible to get the date based off the day of the week) |
Yy |
2 digit year (if greater than, would return 1900 ' s, Else ' s) |
YYYY |
4 digit Year |
A or a |
am/pm |
H, HH |
Hour time |
H, or HH |
Hour time (use of conjunction with a or a) |
M or mm |
Minutes |
S or SS |
Seconds |
S |
Deciseconds (1/10th of a second) |
Ss |
Centiseconds (1/100th of a second) |
Sss |
Milliseconds (1/1000th of a second) |
Z or ZZ |
Timezone offset as ' +0700 ' or ' +07:30 ' Available in version *1.2.0* |
Key |
Shorthand |
Year |
Y |
Month |
M |
Week |
W |
Day |
D |
Hour |
H |
Minutes |
M |
Seconds |
S |
Millisecond |
Ms |
2 Modify Time Object 2.1 plus minus
Moment (). Add (1000) plus milliseconds
Moment (). Add (' Days ', 7) plus 7 day
Moment (). Subtract (' Days ', 7); Minus 7 days
Moment (). Add ({days:-7,months:1}); Plus January, minus 7 days.
2.2 Set Value
Moment (). seconds (30); Year/month/date/day/minutes/seconds method
Moment (). seconds (); Take value
Set head/Tail
Moment (). StartOf (' year '); Set to Jan 1 12:00:00.000 pm this year
Moment (). StartOf (' month '); Set to the first of this month, 12:00:00.000 pm
Moment (). StartOf (' Day '); Set to 12:00:00.000 pm 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);
Moment (). EndOf (' year ');
Moment (). Sod (); Day Head
Moment (). EOD (); The end of the day
3 format output
Moment (). Format ("dddd, MMMM do YYYY, h:mm:ss a");
Token |
Output |
Month |
|
M |
1 2 ... 11 12 |
Mo |
1st 2nd ... 11th 12th |
Mm |
01 02 ... 11 12 |
MMM |
Jan ... Dec |
MMMM |
January February ... November December |
Day of Month |
|
D |
1 2 ... 30 30 |
Do |
1st 2nd ... 30th 31st |
Dd |
01 02 ... 30 31 |
Day of the 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 |
Ddd |
Sun Mon ... Fri Sat |
dddd |
Sunday Monday ... Friday Saturday |
Week of year |
|
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 |
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 |
Timezone |
|
Z or ZZ |
EST CST ... MST PST **note:** As of *1.6.0*, the Z/ZZ format tokens has been deprecated. [Read more on it here.] (https://github.com/timrwood/moment/issues/162) |
Z |
-07:00-06:00 +06:00 +07:00 |
Zz |
-0700-0600. +0600 +0700 Z and ZZ is available in version *1.2.0* |
Localized date format |
|
LT |
8:30AM PM LT's available in version *1.3.0* |
L |
07/10/1986 |
LL |
July 10 1986 |
LLL |
July 1986-6:00PM |
Llll |
Saturday, July 1986-6:00pm L, LL, LLL, Llll is available in version *1.3.0* |
4 comparison
var a = Moment ([2007, 0, 29]);
var B = Moment ([2007, 0, 28]);
- A.diff (b, ' days ')//1
5 Chinese
Moment.lang (' Zh-cn ', {
Months: "January _ February _ March _ April _ May _ June _ July _ August _ September _ October _ November _ December". Split ("_"),
Monthsshort: "January _2 month _3 month _4 month _5 month _6 month _7 month _8 month _9 month _10 month _11 month _12 month". Split ("_"),
Weekdays: "Sunday _ Monday _ Tuesday _ Wednesday _ Thursday _ Friday _ Saturday". Split ("_"),
Weekdaysshort: "Sunday _ Monday _ Tuesday _ Wednesday _ Thursday _ Friday _ Saturday". Split ("_"),
Weekdaysmin: "Day _ one _ two _ three _ four _ five _ Six". Split ("_"),
Longdateformat: {
LT: "Ah point mm",
L: "YYYY year MMMD Day",
LL: "YYYY year MMMD Day",
LLL: "yyyy mmmd Day LT",
LLLL: "yyyy mmmd day Ddddlt",
L: "YYYY year MMMD Day",
LL: "YYYY year MMMD Day",
LLL: "yyyy mmmd Day LT",
LLLL: "yyyy mmmd day DDDDLT"
},
Meridiem:function (hour, minute, islower) {
if (Hour < 9) {
Return "Morning";
} else if (Hour < && minute < 30) {
Return "Morning";
} else if (Hour < && minute < 30) {
Return "Noon";
} else if (Hour < 18) {
return "PM";
} else {
Return "Night";
}
},
Calendar: {
Sameday: ' []lt today ',
NextDay: ' [Tomorrow]lt ',
Nextweek: ' [Down]ddddlt ',
Lastday: ' [Yesterday]lt ',
Lastweek: ' [on]ddddlt ',
Sameelse: ' L '
},
Relativetime: {
Future: "Within%s",
Past: "Before%s",
S: "A few seconds",
M: "1 minutes",
MM: "%d minutes",
H: "1 hours",
HH: "%d hours",
D: "1 days",
DD: "%d days",
M: "1 months",
MM: "%d months",
Y: "1 Years",
YY: "%d Years"
}
});
6 overriding extensions
Moment.months monthsshort weekdays Weekdaysshort weekdaysmin longdateformat relativetime Extended Customization
7 Other
Fragmented API
Clone replication
IsValid Authentication Legal
DaysInMonth days per month
Isdst whether to rest day
Isleapyear Leap Year
Official Document: Http://momentjs.com/docs
SOURCE Download: https://github.com/timrwood/moment/
Moment.js Date wrapper class (illustrative example)