年月日借位相减,输入两个时间点的 年月日
【摘要】
/** * 计算两个日期的时长,返回x年x月x日,传入时间戳 */export function getDateDurations (startDate, endDate) { let startYear = parseInt(moment(startDate).format('YYYY')) let startMonth = pa...
-
/**
-
* 计算两个日期的时长,返回x年x月x日,传入时间戳
-
*/
-
export function getDateDurations (startDate, endDate) {
-
let startYear = parseInt(moment(startDate).format('YYYY'))
-
let startMonth = parseInt(moment(startDate).format('MM'))
-
let startDay = parseInt(moment(startDate).format('DD'))
-
let endYear = parseInt(moment(endDate).format('YYYY'))
-
let endMonth = parseInt(moment(endDate).format('MM'))
-
let endDay = parseInt(moment(endDate).format('DD')) + 1
-
let currentMonthDays = 31
-
if ([1,3,4,7,8,10,12].includes(startMonth)) {
-
currentMonthDays = 31
-
} else if ([4,6,9,11].includes(startMonth)) {
-
currentMonthDays = 30
-
} else if (startMonth === 2) {
-
currentMonthDays = 28
-
}
-
-
let monthJ1 = false // 是否需要向上级月份借位
-
let yearJ1 = false // 是否需要向上级年份借位
-
let mDay = endDay - startDay
-
let mMonth = endMonth - startMonth
-
let mYear = endYear - startYear
-
-
if (mDay < 0) {
-
monthJ1 = true
-
mDay = mDay + currentMonthDays
-
}
-
-
if (monthJ1) {
-
mMonth = mMonth - 1
-
}
-
if (mMonth < 0) {
-
yearJ1 = true
-
mMonth = mMonth + 12
-
}
-
if (mYear > 0) {
-
if (yearJ1) {
-
mYear = mYear - 1
-
}
-
}
-
return (mYear ? mYear + '年' : '') + (mMonth ? mMonth + '月' : '') + (mDay ? mDay + '日' : '')
-
}
文章来源: fizzz.blog.csdn.net,作者:拿我格子衫来,版权归原作者所有,如需转载,请联系作者。
原文链接:fizzz.blog.csdn.net/article/details/103263481
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)