[JS] format moment-timezone的时间格式
//conf.js
app.systemConf = {
timezone: app.conf.get('system.timezone'),
};
// yml conf file
system:
useForever: false
nodeProcessNumber: 1
timezone: America/Los_Angeles
// util.js
momentTimezone = require('moment-timezone'),
global.momentZone = function (time) {
if (time) return momentTimezone(time).tz(app.systemConf.timezone);
return momentTimezone().tz(app.systemConf.timezone);
};
// commonService.js
function formatTimeZone(time) {
return time !== null ? momentZone(time).format('MM/DD/YYYY HH:mm:ss') : time;
}
注意下:
w3c有规定了统一的时间格式:
https://www.w3.org/TR/NOTE-datetime
The formats are as follows. Exactly the components shown here must be present, with exactly this punctuation. Note that the "T" appears literally in the string, to indicate the beginning of the time element, as specified in ISO 8601.
Year:
YYYY (eg 1997)
Year and month:
YYYY-MM (eg 1997-07)
Complete date:
YYYY-MM-DD (eg 1997-07-16)
Complete date plus hours and minutes:
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
Complete date plus hours, minutes and seconds:
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
Complete date plus hours, minutes, seconds and a decimal fraction of a
second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
where:
YYYY = four-digit year
MM = two-digit month (01=January, etc.)
DD = two-digit day of month (01 through 31)
hh = two digits of hour (00 through 23) (am/pm NOT allowed)
mm = two digits of minute (00 through 59)
ss = two digits of second (00 through 59)
s = one or more digits representing a decimal fraction of a second
TZD = time zone designator (Z or +hh:mm or -hh:mm)
而也有自定义的时间格式标准:
https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
有很多,但是主要注意区分:
小时: 大写H(24小时制)小写h(12小时制),
分钟: m表示分钟,只有小写。
虽然也有大写M, 但表示的是月.