moment.js时间加减处理

时间处理其实还蛮麻烦的,但是用了moment.js之后,是真的强大

console.log(moment().format("YYYY-MM-DD HH:mm:ss")); //当前时间
console.log(moment().format("YYYY-MM-DD HH:mm:ss").set('hour', 18).set('minute', 0).set('second', 0)); //当前特定时间
console.log(moment().subtract(10, "days").format("YYYY-MM-DD"));    //当前时间的前10天时间
console.log(moment().subtract(1, "years").format("YYYY-MM-DD"));    //当前时间的前1年时间
console.log(moment().subtract(3, "months").format("YYYY-MM-DD"));   //当前时间的前3个月时间
console.log(moment().subtract(1, "weeks").format("YYYY-MM-DD"));    //当前时间的前一个星期时间
// 同理,之后的为add

console.log(moment().startOf('day').format("YYYY-MM-DD HH:mm:ss"))   //当天开始时间
console.log(moment().startOf('week').format("YYYY-MM-DD HH:mm:ss"))   //当周开始时间
console.log(moment().startOf('month').format("YYYY-MM-DD HH:mm:ss"))   //当月开始时间
console.log(moment().startOf('year').format("YYYY-MM-DD HH:mm:ss"))   //当年开始时间
// 同理,结束为endOf

moment().format('L'); -> 2015-04-06
moment().format('l'); -> 2015-04-06
moment().format('LL'); -> 201546moment().format('ll'); -> 201546moment().format('LLL'); -> 201546日下午355
moment().format('lll'); -> 201546日下午355
moment().format('LLLL'); -> 201546日星期一下午355
moment().format('llll'); -> 201546日星期一下午355

moment.locale('cn', {
  weekdays : [
    "星期天","星期一", "星期二", "星期三", "星期四", "星期五", "星期六", 
  ]
});

// 获取两个时间段之间的月份
function calDiffMonths(StartDate, EndDate) {
  let CurrentMonth = StartDate.startOf('month');
  let Months = [];
  while (CurrentMonth <= EndDate) {
    Months.push(CurrentMonth.clone());
    CurrentMonth = CurrentMonth.add(1, 'months');
  }
  return Months;
}

calDiffMonths(moment('2016-01-01'),moment('2019-01-01'))
calDiffMonths(moment('2016-01-01'),moment('2016-01-01'))

两个日期/时间的时差加减方法:

moment(endTime).diff(moment(startTime), 'years')

moment(endTime).diff(moment(startTime), 'months')

moment(endTime).diff(moment(startTime), 'days')    //  开始时间和结束时间的时间差,以“天”为单位;endTime和startTime都是毫秒数

moment(endTime).diff(moment(startTime),'minutes' )

moment(endTime).diff(moment(startTime), 'seconds')

在Vue中具体怎么用?
先在.vue文件中import进去

import moment from 'moment'

在data中添加moment,切记这个,不然直接在双括号中渲染不出来!!

data () {
	return {
		moment
	}
}

可以直接在标签中使用

<div>{{ moment(youData).format('YYYYY-MM-DD HH:mm') }}</div>
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要计算当天时间差,可以使用Moment.js库中的`moment().startOf('day')`方法。这个方法可以将当前时间设为当天的开始时间,然后再用`moment().diff(moment().startOf('day'), 'milliseconds')`方法计算当前时间和当天开始时间时间差,单位为毫秒。这样就可以得到当天的时间差了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [moment.js使用](https://blog.csdn.net/qq_53689264/article/details/131207555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [moment.js时间处理](https://blog.csdn.net/qq_27674439/article/details/117708915)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [moment.js 常用用法及时区转换](https://blog.csdn.net/weixin_45941959/article/details/122673582)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值