dengjie : 时长,处理耗时的工具类date.js #18

Merged
odjbin merged 1 commits from DJ into master 2023-02-16 17:19:23 +00:00
3 changed files with 41 additions and 33 deletions

View File

@ -1,4 +1,6 @@
//时间转换为String类型
const moment = require("moment");
function simpleDateFormat(pattern) {
var fmt = new Object();
fmt.pattern = pattern;
@ -62,9 +64,9 @@ function simpleDateFormatByMoreLine(date) {
/**
* @param dateBegin 开始时间
* @param dateEnd 结束时间
* @returns {string} 时间差
* @returns {string} 时间差 小时 分钟
*/
function consumingTime(dateBegin,dateEnd) {
function consumingTime(dateBegin, dateEnd) {
// //如果时间格式是正确的,那下面这一步转化时间格式就可以不用了
let submitTime = new Date(dateBegin)
let endTime = new Date(dateEnd)
@ -81,7 +83,36 @@ function consumingTime(dateBegin,dateEnd) {
return dayDiff + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒";
}
function timeLength(start, dateEnd) {
// //如果时间格式是正确的,那下面这一步转化时间格式就可以不用了
let mstart = moment(start);
let mend = moment(dateEnd);
let years = mend.diff(start, "years");
let months = mend.diff(start, "months");
let days = mend.diff(start, "days");
let hours = mend.diff(start, "hours");
let minutes = mend.diff(start, "minutes");
minutes = minutes % 60;
hours = hours % 24;
months = months % 12;
//因为每月天不固定,所以天要特殊动态处理
if (mstart.date() < mend.date()) {
days = mend.date() - mstart.date();
if (minutes > 0 || hours > 0) {
days--;
}
}
//处理超过俩月且天超过31
if (days > 31 && mend.month() - mstart.month() >= 2) {
//将日期推至上月求差
days = mend.diff(mstart.add(mend.month() - mstart.month() - 1, "month"), "days");
}
return `${years > 0 ? years + "年 " : " "}` + `${months > 0 ? months + "个月 " : " "}` + `${days > 0 ? days + "天 " : " "}`
+ `${hours > 0 ? hours + "小时 " : " "}` + `${minutes > 0 ? minutes + "分钟 " : " "}`;
}
module.exports = {
formatToYYYYMMDD: simpleDateFormatByMoreLine,
consumingTime: consumingTime
consumingTime: consumingTime,
timeLength: timeLength
}

View File

@ -37,6 +37,7 @@
<script>
import componentMinxins from "../ComponentMinxins";
import moment from "moment";
import {timeLength} from '@/utils/date'
export default {
mixins: [componentMinxins],
name: "DateTimeRange",
@ -85,36 +86,12 @@ export default {
timeLength() {
//
if (Array.isArray(this.value)) {
let start = moment(this.value[0]).format(this.format.replaceAll("dd", "DD"));
let end = moment(this.value[1]).format(this.format.replaceAll("dd", "DD"));
if (start === end) {
// let start = moment(this.value[0]).format(this.format.replaceAll("dd", "DD"));
// let end = moment(this.value[1]).format(this.format.replaceAll("dd", "DD"));
if (this.value[0] === this.value[1]) {
return "0 时长为0请确认";
}
let mstart = moment(start);
let mend = moment(end);
let years = mend.diff(start, "years");
let months = mend.diff(start, "months");
let days = mend.diff(start, "days");
let hours = mend.diff(start, "hours");
let minutes = mend.diff(start, "minutes");
minutes = minutes % 60;
hours = hours % 24;
months = months % 12;
//
if (mstart.date() < mend.date()) {
days = mend.date() - mstart.date();
if (minutes > 0 || hours > 0) {
days--;
}
}
//31
if (days > 31 && mend.month() - mstart.month() >= 2) {
//
days = mend.diff(mstart.add(mend.month() - mstart.month() - 1, "month"), "days");
}
return `${years > 0 ? years + "年 " : " "}${months > 0 ? months + "个月 " : " "}
${days > 0 ? days + "天 " : " "}${hours > 0 ? hours + "小时 " : " "}
${minutes > 0 ? minutes + "分钟 " : " "}`;
return timeLength(this.value[0],this.value[1])
} else {
return "先选择时间哦";
}

View File

@ -88,7 +88,7 @@ import Comment from "@/components/common/Comment";
import FormRenderView from '@/views/common/form/FormRender'
import ProcessDiagramViewer from "../admin/layout/ProcessDiagramViewer";
import {getInitiatedInstanceList, getInitiatedInstanceInfo} from "@/api/processInstance";
import {consumingTime} from '@/utils/date'
import {timeLength} from '@/utils/date'
export default {
name: "InitiatedInstance",
components: {Comment, FormRenderView, ProcessDiagramViewer},
@ -156,7 +156,7 @@ export default {
getTimeConsuming(instance) {
if (instance.state != 1) {
//dateFormat(,)
return consumingTime(instance.submitTime,instance.endTime);
return timeLength(instance.submitTime,instance.endTime);
}
}
}