Skip to main content

Date & Time

// 有 daylight savings 的时区可能有问题
// https://github.com/iamkun/dayjs/issues/1260
// 目前 luxon 能处理好这样的情况
dayjs.utc('2020-03-04T10:00:00Z').tz('America/New_York').add(1, 'week').$offset;
// offset should be -240 since DST happens on 2020-03-05
// but the offset remains what the starting offset is, (-300)

// luxon 结果为 -240
luxon.DateTime.fromISO('2020-03-04T10:00:00Z', { zone: 'utc' }).setZone('America/New_York').plus({ weeks: 1 }).offset;
abbr.stand formean
UTCCoordinated Universal Time协调世界时
GMTGreenwich Mean Time格林尼治标准时间

Format

  • new Date().toString()
  • Day MMM DD YYYY HH:mm:ss GMT±HHMM (TimeZone)
// Day MMM DD YYYY HH:mm:ss GMT±HHMM (TimeZone)
// Fri Oct 08 2010 07:06:05 GMT+0800 (China Standard Time)
new Date(2010, 9, 8, 7, 6, 5).toString();

// RFC 2822 常用于电子邮件
// HTTP 头
// Day, DD MMM YYYY HH:mm:ss GMT
// Thu, 07 Oct 2010 23:06:05 GMT
new Date(2010, 9, 8, 7, 6, 5).toGMTString();
meaningCLDRnotes
eraGAD
yeary989,2017
yy89,17
monthMSeptember
LStandalone
Day of weekETuesday
cStandalone
dayd
hourh12h
H0-23h
K0-11h
k1-24h
minutem
seconds
day periodaAM/PM
b
B
timezonez/v
QuarterQ
'

Duration

unitforcn
yyear
Mmonth
wweek
dday
hhour小时
mminute分钟
ssecond
Qquarter季度

Relative Time

  • Grafana
    • now-6h - 6 小时前

Grafana

  • isRelativeTimeRange
    • 只要包含 now 就是
  • 默认 number 为 秒 单位
export function getDefaultTimeRange(): TimeRange {
const now = dateTime();

return {
from: dateTime(now).subtract(6, 'hour'),
to: now,
raw: { from: 'now-6h', to: 'now' },
};
}
fromtodisplaycn
now/dnow/dToday今天
now/dnowToday so far今天到目前为止
now/wnow/wThis week本周
now/wnowThis week so far本周到目前为止
now/Mnow/MThis month本月
now/MnowThis month so far本月到目前为止
now/ynow/yThis year今年
now/ynowThis year so far今年到目前为止
now-1d/dnow-1d/dYesterday昨天
now-2d/dnow-2d/dDay before yesterday前天
now-7d/dnow-7d/dThis day last week上周的这一天
now-1w/wnow-1w/wPrevious week上周
now-1M/Mnow-1M/MPrevious month上个月
now-1Q/fQnow-1Q/fQPrevious fiscal quarter上个财季
now-1y/ynow-1y/yPrevious year去年
now-1y/fynow-1y/fyPrevious fiscal year上个财年
now-5mnowLast 5 minutes过去5分钟
now-15mnowLast 15 minutes过去15分钟
now-30mnowLast 30 minutes过去30分钟
now-1hnowLast 1 hour过去1小时
now-3hnowLast 3 hours过去3小时
now-6hnowLast 6 hours过去6小时
now-12hnowLast 12 hours过去12小时
now-24hnowLast 24 hours过去24小时
now-2dnowLast 2 days过去2天
now-7dnowLast 7 days过去7天
now-30dnowLast 30 days过去30天
now-90dnowLast 90 days过去90天
now-6MnowLast 6 months过去6个月
now-1ynowLast 1 year过去1年
now-2ynowLast 2 years过去2年
now-5ynowLast 5 years过去5年
now/fQnowThis fiscal quarter so far本财季到目前为止
now/fQnow/fQThis fiscal quarter本财季
now/fynowThis fiscal year so far本财年到目前为止
now/fynow/fyThis fiscal year本财年
fromtodisplaycn
nownow+1mNext minute下一分钟
nownow+5mNext 5 minutes接下来的5分钟
nownow+15mNext 15 minutes接下来的15分钟
nownow+30mNext 30 minutes接下来的30分钟
nownow+1hNext hour下一小时
nownow+3hNext 3 hours接下来的3小时
nownow+6hNext 6 hours接下来的6小时
nownow+12hNext 12 hours接下来的12小时
nownow+24hNext 24 hours接下来的24小时
nownow+2dNext 2 days接下来的2天
nownow+7dNext 7 days接下来的7天
nownow+30dNext 30 days接下来的30天
nownow+90dNext 90 days接下来的90天
nownow+6MNext 6 months接下来的6个月
nownow+1yNext year下一年
nownow+2yNext 2 years接下来的2年
nownow+5yNext 5 years接下来的5年

MomentJS

moment.updateLocale('en', {
relativeTime: {
future: 'in %s',
past: '%s ago',
s: 'a few seconds',
ss: '%d seconds',
m: 'a minute',
mm: '%d minutes',
h: 'an hour',
hh: '%d hours',
d: 'a day',
dd: '%d days',
w: 'a week',
ww: '%d weeks',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years',
},
});