nicecode-v2/packages/func/es/time/index.js

57 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//时间差计算
import dayjs from "dayjs";
/*
* startDate==>开始时间
* endDate==>结束时间
* 事例diffTime(data.createTime,new Date())
*
* */
export var formateDuration = function formateDuration(diff) {
//计算出相差天数
var days = Math.floor(diff / (24 * 3600 * 1000));
//计算出小时数
var leave1 = diff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
var hours = Math.floor(leave1 / (3600 * 1000));
//计算相差分钟数
var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
var minutes = Math.floor(leave2 / (60 * 1000));
//计算相差秒数
var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
var seconds = Math.round(leave3 / 1000);
var returnStr = seconds + '秒';
if (minutes > 0) {
returnStr = minutes + '分'; //+ returnStr;
}
if (hours > 0) {
returnStr = hours + '小时'; // + returnStr;
}
if (days > 0) {
returnStr = days + '天'; //+ returnStr;
}
return returnStr;
};
//检索默认时间
export function generateTime() {
var endDateTime = dayjs().endOf('day').unix();
var startDateTime = dayjs().startOf('day').unix();
return {
startDateTime: startDateTime,
endDateTime: endDateTime
};
}
/**
* 格式化时间为 00:00
* @param seconds 时间,单位秒
* @returns
*/
export function formatDurationTime(seconds) {
var minutes = Math.floor(seconds / 60) || 0;
var remainingSeconds = Math.floor(seconds % 60);
return (minutes < 10 ? "0".concat(minutes) : minutes) + ":" + (remainingSeconds < 10 ? "0" : "") + remainingSeconds;
}