# 自定义
Moment.js is very easy to customize. In general, you should create a locale setting with your customizations.
moment.locale('en-my-settings', {
// customizations.
});
2
3
However, you can also overwrite an existing locale that has been loaded as well.
moment.locale('en', {
// customizations
});
2
3
Any settings that are not defined are inherited from the default english settings.
You can remove a previously defined locale by passing null as 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'
2
3
4
# 月份名
// 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
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Locale#monthsshould be anarray of the month names.
moment.locale('en', {
months : [
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
]
});
2
3
4
5
6
If you need more processing to calculate the name of the month, (for example, if there is different grammar for different formats),
Locale#months can be a functionwith 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()];
}
}
});
2
3
4
5
6
7
8
9
10
11
# 月份名_缩写
// 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
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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"
]
});
2
3
4
5
6
Like Locale#months, Locale#monthsShort can 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()];
}
}
});
2
3
4
5
6
7
8
9
# 星期名
// 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
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Locale#weekdaysshould be an array of the weekdays names.
moment.locale('en', {
weekdays : [
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
]
});
2
3
4
5
Locale#weekdays can be a callback function as well.
moment.locale('en', {
weekdays : function (momentToFormat, format) {
return weekdays[momentToFormat.day()];
}
});
2
3
4
5
# 星期名_缩写
// 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
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Locale#weekdaysShort should be an array of the weekdays abbreviations.
moment.locale('en', {
weekdaysShort : ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
});
2
3
Locale#weekdaysShort can be a callback function as well.
moment.locale('en', {
weekdaysShort : function (momentToFormat, format) {
return weekdaysShort[momentToFormat.day()];
}
});
2
3
4
5
# 星期名_极简缩写
// 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
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Locale#weekdaysMin should 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"]
});
2
3
Locale#weekdaysMincan be a callback function as well.
moment.locale('en', {
weekdaysMin : function (momentToFormat, format) {
return weekdaysMin[momentToFormat.day()];
}
});
2
3
4
5
# 长日期格式
// From 2.8.1 onward
moment.locale('en', {
longDateFormat : Object
});
// Deprecated in 2.8.1
moment.lang('en', {
longDateFormat : Object
});
2
3
4
5
6
7
8
9
Locale#longDateFormatshould be an object containing a key/value pair for each long date format L LL LLL LLLL LT LTS. LT should 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"
}
});
2
3
4
5
6
7
8
9
10
11
12
13
14
You can eliminate the lowercase l tokens and they will be created automatically by replacing long tokens with the short token variants.
您可以消除小写的l标记,它们将通过使用短标记变体替换长标记而自动创建。
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"
}
});
2
3
4
5
6
7
8
9
10
# 月相对时间份名
// From 2.8.1 onward
moment.locale('en', {
relativeTime : Object
});
// Deprecated in 2.8.1
moment.lang('en', {
relativeTime : Object
});
2
3
4
5
6
7
8
9
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"
}
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Locale#relativeTime.future refers to the prefix/suffix for future dates, and Locale#relativeTime.past refers 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.Locale#relativeTime.future引用将来日期的前缀/后缀,而Locale#relativeTime.past引用过去日期的前缀/后缀。对于所有其他字符,单个字符是指单数,而双字符是指复数。
如果语言环境需要对令牌进行其他处理,则可以使用以下签名将令牌设置为功能。该函数应返回一个字符串。
function (number, withoutSuffix, key, isFuture) {
return string;
}
2
3
The key argument refers to the replacement key in the
Locale#relativeTimeobject. (eg. s m mm h, etc.)
The number argument refers to the number of units for that key. For m, the number is the number of minutes, etc.
The withoutSuffix argument 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 isFuture argument 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 isFuture argument 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
});
2
3
4
5
6
7
8
9
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#meridiem should 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 "晚上";
}
}
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- Before version 1.6.0, Locale#meridiem was a map of upper and lowercase versions of am/pm.
moment.locale('en', {
meridiem : {
am : 'am',
AM : 'AM',
pm : 'pm',
PM : 'PM'
}
});
2
3
4
5
6
7
8
- 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
});
2
3
4
5
6
7
8
9
10
11
Locale#isPMshould return true if the input string is past 12 noon. This is used in parsing the a A tokens.
moment.locale('en', {
isPM : function (input) {
return ((input + '').toLowerCase()[0] === 'p');
}
});
2
3
4
5
To configure what strings should be parsed as input, set the meridiemParse property.
moment.locale('en', {
meridiemParse : /[ap]\.?m?\.?/i
});
2
3
# 日历 1.3.0+
// From 2.8.1 onward
moment.locale('en', {
calendar : Object
});
// Deprecated in 2.8.1
moment.lang('en', {
calendar : Object
});
2
3
4
5
6
7
8
9
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'
}
});
2
3
4
5
6
7
8
9
10
Each of the Locale#calendar keys 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';
},
2
3
# 比较 1.0.0+
// From 2.8.1 onward
moment.locale('en', {
ordinal : Function
});
// Deprecated in 2.8.1
moment.lang('en', {
ordinal : Function
});
2
3
4
5
6
7
8
9
Locale#ordinal should 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;
}
});
2
3
4
5
6
7
8
9
10
- 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: M or d.
- For more information on ordinal numbers, see wikipedia
# 相对时间临界值 2.7.0+
moment.relativeTimeThreshold(unit); // getter
moment.relativeTimeThreshold(unit, limit); // setter
2
duration.humanize has 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.
duration.humanize具有阈值,这些
阈值定义何时将一个单位视为分钟,一个小时等。例如,默认情况下,超过45秒被视为一分钟,超过22小时被视为一天,依此类推。要更改这些截止值,请使用moment.relativeTimeThreshold(unit,limit),其中limit是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);
2
3
4
5
6
7
8
9
10
11
12
13
- NOTE: Retrieving thresholds was added in 2.8.1.