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
Showing only changes of commit 662c0188e8 - Show all commits

View File

@ -1,4 +1,6 @@
//时间转换为String类型 //时间转换为String类型
const moment = require("moment");
function simpleDateFormat(pattern) { function simpleDateFormat(pattern) {
var fmt = new Object(); var fmt = new Object();
fmt.pattern = pattern; fmt.pattern = pattern;
@ -62,9 +64,9 @@ function simpleDateFormatByMoreLine(date) {
/** /**
* @param dateBegin 开始时间 * @param dateBegin 开始时间
* @param dateEnd 结束时间 * @param dateEnd 结束时间
* @returns {string} 时间差 * @returns {string} 时间差 小时 分钟
*/ */
function consumingTime(dateBegin,dateEnd) { function consumingTime(dateBegin, dateEnd) {
// //如果时间格式是正确的,那下面这一步转化时间格式就可以不用了 // //如果时间格式是正确的,那下面这一步转化时间格式就可以不用了
let submitTime = new Date(dateBegin) let submitTime = new Date(dateBegin)
let endTime = new Date(dateEnd) let endTime = new Date(dateEnd)
@ -81,7 +83,36 @@ function consumingTime(dateBegin,dateEnd) {
return dayDiff + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒"; 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 = { module.exports = {
formatToYYYYMMDD: simpleDateFormatByMoreLine, formatToYYYYMMDD: simpleDateFormatByMoreLine,
consumingTime: consumingTime consumingTime: consumingTime,
timeLength: timeLength
} }

View File

@ -37,6 +37,7 @@
<script> <script>
import componentMinxins from "../ComponentMinxins"; import componentMinxins from "../ComponentMinxins";
import moment from "moment"; import moment from "moment";
import {timeLength} from '@/utils/date'
export default { export default {
mixins: [componentMinxins], mixins: [componentMinxins],
name: "DateTimeRange", name: "DateTimeRange",
@ -85,36 +86,12 @@ export default {
timeLength() { timeLength() {
// //
if (Array.isArray(this.value)) { if (Array.isArray(this.value)) {
let start = moment(this.value[0]).format(this.format.replaceAll("dd", "DD")); // let start = moment(this.value[0]).format(this.format.replaceAll("dd", "DD"));
let end = moment(this.value[1]).format(this.format.replaceAll("dd", "DD")); // let end = moment(this.value[1]).format(this.format.replaceAll("dd", "DD"));
if (start === end) { if (this.value[0] === this.value[1]) {
return "0 时长为0请确认"; return "0 时长为0请确认";
} }
let mstart = moment(start); return timeLength(this.value[0],this.value[1])
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 + "分钟 " : " "}`;
} else { } else {
return "先选择时间哦"; return "先选择时间哦";
} }

View File

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