【ECMAScript】日期时间处理

http://momentjs.cn/

npm install moment --save   # npm
yarn add moment             # Yarn
Install-Package Moment.js   # NuGet
spm install moment --save   # spm
meteor add momentjs:moment  # meteor
bower install moment --save # bower (废弃)

moment(new Date()).format('YYYY-MM-DD HH:mm:ss'); // 2021-01-20 10:58:45
moment(new Date()).format('MM月DD日'); // 01月20日
moment(new Date()).format('MMM'); // 1月
moment(new Date()).format('MMMM'); // 一月
moment(new Date()).format('dd'); //
moment(new Date()).format('ddd'); // 周三
moment(new Date()).format('dddd'); //  星期三
moment(new Date()).isoWeekday(); // 3
moment(new Date()).isoWeekYear(); // 2021
moment(new Date()).format('LT'); // 10:58
moment(new Date()).format('LTS'); // 10:58:45
moment(new Date()).format('L'); // 2021/01/20
moment(new Date()).format('LL'); //  2021年1月20日
moment(new Date()).format('LLL'); // 2021年1月20日上午10点58分
moment(new Date()).format('LLLL'); // 2021年1月20日星期三上午10点58分
moment(new Date()).format('l'); // 2021/1/20
moment(new Date()).format('ll'); // 2021年1月20日
moment(new Date()).format('lll'); // 2021年1月20日 10:58
moment(new Date()).format('llll'); // 2021年1月20日星期三 10:58
moment(new Date()).format('A'); // 上午
moment(new Date()).format('a'); // 上午
moment(new Date()).format('ALT') // 上午10:58

// subtract 减法 、 add 加法
moment().add(7, 'days').format('LL'); // 当前日期的7天后的日期 
moment().subtract(7, 'days').format('LL'); // 当前日期的7天前的日期
moment().add(9, 'hours').format('HH:mm:ss'); // 当前时间的9小时后 
moment().subtract(9, 'hours').format('HH:mm:ss'); // 当前时间的9小时前 
moment().add(1, 'week').format('LL'); // 当前日期的1周后

// fromNow 时差 (之前) ; fromNow(true)  加true表示去除前字或者内字,不加则保留
moment([2017, 0, 29]).fromNow(true); //  4年
moment([2017, 0, 29]).fromNow(); //  4年前
moment([2020, 8, 29]).fromNow(true); //  5个月
moment([2020, 8, 29]).fromNow(); //  5个月内
moment("20170929", "YYYYMMDD").fromNow(); // 6年前
moment(+new Date() - 1000 * 300).fromNow(); // 5分钟前
moment(+new Date() - 1000 * 3).fromNow(); // 几秒前
moment(+new Date() - 3 * 24 * 60 * 60 * 1000).fromNow(); // 3天前
moment(+new Date() - 30 * 24 * 60 * 60 * 1000).fromNow(); // 1个月前
moment(+new Date() - 365 * 24 * 60 * 60 * 1000).fromNow(); // 1年前

// toNow 时差 (之后 现在为基准) ; toNow(true)  去除前或者内字
moment([2017, 0, 29]).toNow(); // 4年内
moment([2020, 0, 29]).toNow(); // 1年内
moment([2020, 0, 29]).toNow(true); // 1年

// 时差 (之后; to(true)  // 去除前或者内字  (注意 这个可能会出错),个人认为没有fromNow(true)好用
new moment([2017, 0, 29]).to(); // 4年内
new moment([2020, 0, 29]).to(); // 1年前
new moment([2020, 0, 29]).to(); // 1年
// 时差 (毫秒) 
moment([2007, 0, 29]).diff(moment([2007, 0, 28])); //  相差一天的毫秒数86400000
// 时差 (分钟) 
moment([2007, 0, 29]).diff(moment([2007, 0, 28]), 'minute'); //一天相差1440分钟
// 时差 (天) 
moment([2007, 0, 29]).diff(moment([2007, 0, 28]), 'days'); //  1天
// 天数 (月) 
moment("2025-02", "YYYY-MM").daysInMonth(); //  29   计算当月有多少天
//时间差
let m1 = moment('2018-03-30 18:13:12') //指定一个时间
let m2 = moment()
var du = moment.duration(m2 - m1, 'ms'),
years = du.get('year'),
months = du.get('months'),
days = du.get('days'),
hours = du.get('hours'),
mins = du.get('minutes'),
ss = du.get('seconds');
console.log(years + "年" + months + "月" + days + "日" + hours + '时' + mins + '分' + ss + '秒');

 

posted @ 2022-02-17 14:25  翠微  阅读(46)  评论(0编辑  收藏  举报