ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【ECMAScript】日期时间处理

2022-02-17 14:31:50  阅读:192  来源: 互联网

标签:format 处理 moment 29 日期 ECMAScript Date new fromNow


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 + '秒');

 

标签:format,处理,moment,29,日期,ECMAScript,Date,new,fromNow
来源: https://www.cnblogs.com/yangchongxing/p/15904504.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有