vue中Momentjs计算两个时间差值表现为⼈类可读格式(转)我们知道momentjs是⼀个很强⼤的时间计算⼯具,⽤起来真的很⽅便。
我在⼀次项⽬开发的时候要求通过选择⼀个⽇期,来计算这个⼈的年龄:格式为xx岁xx个⽉xx天,即⼈类可读格式。
我们知道moment有个diff算法,但是这个不满⾜我的需求,它虽然能获取两个时间之间差值时长,但是⽤起来不是很⽅便,贴下它⽤法吧:
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1
然后,在经过我继续翻⽂档的时候发现了⼀款插件:
我们可以直接npm安装:
npm install moment-precise-range-plugin
也可以把⽂件下载下来,他只是⼀个js⽂件,代码其实没多少,我贴⼀下:
if (typeof moment === "undefined" && typeof require === "function") {
var moment = require("moment");
}
(function(moment) {
var STRINGS = {
nodiff: "",
year: "year",
years: "岁",
month: "month",
months: "个⽉",
day: "day",
days: "天",
hour: "hour",
hours: "hours",
minute: "minute",
minutes: "minutes",
second: "second",
seconds: "seconds",
delimiter: " "
};
function pluralize(num, word) {
return num + " " + STRINGS[word + (num === 1 ? "" : "s")];
}
function buildStringFromValues(
yDiff,
mDiff,
dDiff,
hourDiff,
minDiff,
secDiff
) {
var result = [];
if (yDiff) {
result.push(pluralize(yDiff, "year"));
}
if (mDiff) {
result.push(pluralize(mDiff, "month"));
}
if (dDiff) {
if (dDiff) {
result.push(pluralize(dDiff, "day"));
}
if (hourDiff) {
result.push(pluralize(hourDiff, "hour"));
}
if (minDiff) {
result.push(pluralize(minDiff, "minute"));
}
if (secDiff) {
result.push(pluralize(secDiff, "second"));
}
return result.join(STRINGS.delimiter);
}
function buildValueObject(
yDiff,
mDiff,
dDiff,
hourDiff,
minDiff,
secDiff,
firstDateWasLater
) {
return {
years: yDiff,
months: mDiff,
days: dDiff,
hours: hourDiff,
minutes: minDiff,
seconds: secDiff,
firstDateWasLater: firstDateWasLater
};
}
moment.fn.preciseDiff = function(d2, returnValueObject) {
return moment.preciseDiff(this, d2, returnValueObject);
};
moment.preciseDiff = function(d1, d2, returnValueObject) {
var m1 = moment(d1),
m2 = moment(d2),
firstDateWasLater;
m1.add(m2.utcOffset() - m1.utcOffset(), "minutes"); // shift timezone of m1 to m2
if (m1.isSame(m2)) {
if (returnValueObject) {
return buildValueObject(0, 0, 0, 0, 0, 0, false);
} else {
diff;
}
}
if (m1.isAfter(m2)) {
var tmp = m1;
m1 = m2;
m2 = tmp;
firstDateWasLater = true;
} else {
firstDateWasLater = false;
}
var yDiff = m2.year() - m1.year();
var mDiff = m2.month() - m1.month();
var dDiff = m2.date() - m1.date();
var hourDiff = m2.hour() - m1.hour();
var hourDiff = m2.hour() - m1.hour();
var minDiff = m2.minute() - m1.minute();
var secDiff = m2.second() - m1.second();
if (secDiff < 0) {
secDiff = 60 + secDiff;
minDiff--;
}
if (minDiff < 0) {
minDiff = 60 + minDiff;
hourDiff--;
}
if (hourDiff < 0) {
hourDiff = 24 + hourDiff;
dDiff--;
}
if (dDiff < 0) {
var daysInLastFullMonth = moment(
"YYYY-MM"
)
.subtract(1, "M")
.
daysInMonth();
if (daysInLastFullMonth < m1.date()) {
// 31/01 -> 2/03
dDiff = daysInLastFullMonth + dDiff + (m1.date() - daysInLastFullMonth);
} else {
dDiff = daysInLastFullMonth + dDiff;
}
mDiff--;
}
if (mDiff < 0) {
mDiff = 12 + mDiff;
yDiff--;
}
if (returnValueObject) {
return buildValueObject(
yDiff,
mDiff,
dDiff,
hourDiff,
minDiff,
secDiff,
firstDateWasLater
)
;
} else {
return buildStringFromValues(
yDiff,
mDiff,
dDiff
// hourDiff,
// minDiff,
// secDiff
typeof的用法);
}
};
})(moment);
这⾥我把源码做了下⼩改动,只是输出的⽇期单位改了改,问题不⼤,⼤家⽤的时候⾃⾏下载哈。然后说下使⽤⽅法:
main.js中引⼊插件:我是下的js⽂件
require("@/utils/moment-precise-range.js"); //加载moment插件
然后我⼜在utils.js中封装了⼀个⽅法:
/**
*
* @param {String} startTime "2019-01-10"
* @param {String} endTime 时间戳10位
*/
computedTime(startTime = "", endTime) {
let st = startTime == "" ? moment().format("YYYY-MM-DD") : startTime;
let s_d = moment(this.formatDate(endTime), "YYYY-MM-DD");
let e_d = moment(st, "YYYY-MM-DD");
return moment
.preciseDiff(s_d, e_d)
.replace("year", "岁")
.replace("month", "个⽉")
.replace("day", "天");
},
当我们想知道某个⼈的年龄时,可以这样算:
let age = computedTime("",birthday );
当然,我还⽤它来获取合同临期,试⽤期临期等⼀些时间段值。
好了,写的不是很好,有问题或者您有更好的⽅法请给博主留⾔,欢迎哦~更多请浏览其它⽂章^_^!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论