moment对象的常用方法,看完你就能为所欲为

moment 使用方法

moment需要先引入,再使用,其本身是个对象,内置了一些方法,可以把字符串,Date对象转化成moment对象

moment 用法展示

import moment from 'moment';

const date = new Date;

console.log('按照format中的格式显示日期');
console.log('moment(date).format(YYYY-MM-DD HH:mm:ss)', moment(date).format('YYYY-MM-DD HH:mm:ss'));
console.log('');

console.log('按照format中的格式显示日期,其中formate中的参数可以更改为固定值');
console.log('moment(date).format(YYYY-MM-01 00:00:00)', moment(date).format('YYYY-MM-01 00:00:00'));
console.log('');

console.log('日期所在周的周日');
console.log('moment(date).endOf(week)', moment(date).endOf('week').format('YYYY-MM-DD 00:00:00'));
console.log('');

console.log('日期所在周的周一');
console.log('moment(date).startOf(week)', moment(date).startOf('week').format('YYYY-MM-DD 00:00:00'));
console.log('');

console.log('日期的小时数');
console.log('moment(date).hour()', moment(date).hour());
console.log('');

console.log('日期的月份数(从0开始到11)');
console.log('moment(date).month()', moment(date).month());
console.log('');

console.log('日期的所在周是第几周');
console.log('moment(date).week()', moment(date).week());
console.log('');

console.log('日期的星期数');
console.log('moment(date).day()', moment(date).day());
console.log('');

console.log('日期的日数');
console.log('moment(date).date()', moment(date).date());
console.log('');

console.log('日期的六天前');
console.log('moment(date).add(-6, days)', moment(date).add(-6, 'days').format('YYYY-MM-DD 00:00:00'));
console.log('');

console.log('日期所在月的最后一天');
console.log('moment(date).endOf(month)', moment(date).endOf('month').format('YYYY-MM-DD 00:00:00'));
console.log('');

console.log('日期的下一天');
console.log('moment(date).add(1, days)', moment(date).add(1, 'days').format('YYYY-MM-DD 00:00:00'));
console.log('');

console.log('日期的中国标准时间输出格式');
console.log('moment(date)._d', moment(date)._d);
console.log('');

console.log('日期所在月有多少天');
console.log('moment(date).daysInMonth()', moment(date).daysInMonth());
console.log('');

输出结果查看

在这里插入图片描述
在这里插入图片描述

发布了30 篇原创文章 · 获赞 6 · 访问量 4720

猜你喜欢

转载自blog.csdn.net/EcbJS/article/details/105437436