1.安装或引入

// node.js
// npm install moment 安装
let moment = require('moment'); 或 import moment from 'moment';
moment().format();

// 浏览器中
<script src="moment.js"></script>
<script>
    moment().format();
</script>

2.常用方法说明

import moment from 'moment';

var now: any = moment().toDate();
console.log('获取当前时间\n', now)

now = moment().format('YYYY-MM-DD');
console.log('格式化当前时间\n', now);

now = moment().format('YYYY-MM-DD:HH:MM:SS');
console.log('格式化当前时间\n', now);

// 要转义格式字符串中的字符,可以将字符包装在方括号中。
console.log('转义字符\n',moment().format('[今天] dddd'));


console.log('正确格式\n', moment("2018-11-01").isValid());
console.log('错误格式\n', moment("12:12:12").isValid());

let startMonth = moment().startOf('month').toDate();
console.log('获取这个月初时间\n', startMonth);

let dayOfStart = moment().startOf('day').toDate();
console.log('获取今天开始的时间\n', dayOfStart);

let dayOfEnd = moment().endOf('day').toDate();
console.log('获取今天结束的时间\n', dayOfEnd);

let lateHour = moment().add(2, 'hour').toDate();
console.log('获取+n小时\n', lateHour);

console.log("------------------------------------")
let beforeHour = moment().subtract(2, 'hour').toDate();
console.log('//获取-n小时\n', beforeHour);

let lateDay = moment().add(+5, 'day').toDate();
console.log('获取+n天\n', lateDay);

let beforeDay = moment().add(-5, 'day').toDate();
console.log('获取-n天\n', beforeDay);
//也可以表示为
beforeDay = moment().subtract(5, 'day').toDate();
console.log(beforeDay);

let lateMonth = moment().add(2, 'month').toDate();
console.log('获取+n月\n', lateMonth);

let beforeMonth = moment().subtract(2, 'month').toDate();
console.log('获取-n月\n', moment(beforeMonth).format('YYYY-MM-DD'));

let week = moment().format('dddd');
console.log('获取星期\n', week);

const startDate =  moment('2021-8-17').format('YYYY-MM-DD');
const endDate = moment('2022-8-25').format('YYYY-MM-DD');
console.log ('获取相差天数',moment(endDate).diff(startDate, 'day'));

let years = moment('2018-11-01').fromNow();
console.log("获取相差年数", years);

3.format格式

默认格式 DD.MM.YYYY HH:mm

通过下面的方法可以修改默认格式

moment.defaultFormat = "DD.MM.YYYY HH:mm"; 

令牌 输出
月份 M 1 2 ... 11 12
Mo 1st 2nd ... 11th 12th
MM 01 02 ... 11 12
MMM Jan Feb ... Nov Dec
MMMM January February ... November December
季度 Q 1 2 3 4
Qo 1st 2nd 3rd 4th
月份的日期 D 1 2 ... 30 31
Do 1st 2nd ... 30th 31st
DD 01 02 ... 30 31
年份的日期 DDD 1 2 ... 364 365
DDDo 1st 2nd ... 364th 365th
DDDD 001 002 ... 364 365
星期几 d 0 1 ... 5 6
do 0th 1st ... 5th 6th
dd Su Mo ... Fr Sa
ddd Sun Mon ... Fri Sat
dddd Sunday Monday ... Friday Saturday
星期几(语言环境) e 0 1 ... 5 6
星期几(ISO) E 1 2 ... 6 7
年份的星期 w 1 2 ... 52 53
wo 1st 2nd ... 52nd 53rd
ww 01 02 ... 52 53
年份的星期(ISO) W 1 2 ... 52 53
Wo 1st 2nd ... 52nd 53rd
WW 01 02 ... 52 53
年份 YY 70 71 ... 29 30
YYYY 1970 1971 ... 2029 2030
Y 1970 1971 ... 9999 +10000 +10001
注意:对于 9999 年以后的日期,这符合 ISO 8601 标准。
周年 gg 70 71 ... 29 30
gggg 1970 1971 ... 2029 2030
周年(ISO) GG 70 71 ... 29 30
GGGG 1970 1971 ... 2029 2030
子午线 A AM PM
a am pm
小时 H 0 1 ... 22 23
HH 00 01 ... 22 23
h 1 2 ... 11 12
hh 01 02 ... 11 12
k 1 2 ... 23 24
kk 01 02 ... 23 24
分钟 m 0 1 ... 58 59
mm 00 01 ... 58 59
秒钟 s 0 1 ... 58 59
ss 00 01 ... 58 59
小数秒钟 S 0 1 ... 8 9
SS 00 01 ... 98 99
SSS 000 001 ... 998 999
SSSS ... SSSSSSSSS 000[0..] 001[0..] ... 998[0..] 999[0..]
时区 z or zz EST CST ... MST PST
注意:从 1.6.0 版本开始,z/zz 格式的令牌已从普通的 moment 对象中弃用。 在此处了解更多信息。 但是,如果将特定时区与 moment-timezone 插件一起使用,它们会起作用。
Z -07:00 -06:00 ... +06:00 +07:00
ZZ -0700 -0600 ... +0600 +0700
Unix 时间戳 X 1360013296
Unix 毫秒时间戳 x 1360013296123

4.本地化格式

时间 LT 8:30 PM
带秒钟的时间 LTS 8:30:25 PM
月份数字,月份日期,年份 L 09/04/1986
l 9/4/1986
月份名称,月份日期,年份 LL September 4, 1986
ll Sep 4, 1986
月份名称,月份日期,年份,时间 LLL September 4, 1986 8:30 PM
lll Sep 4, 1986 8:30 PM
月份名称,月份日期,星期几,年份,时间 LLLL Thursday, September 4, 1986 8:30 PM
llll Thu, Sep 4, 1986 8:30 PM
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐