常用时间处理 工具js
export function getDiffMonth(a, b) {
const dt1 = a.split("-"),
dt2 = b.split("-"),
month1 = dt1[0] * 12 + parseInt(dt1[1]),
month2 = dt2[0] * 12 + parseInt(dt2[1]);
return month2 - month1;
}
export function getDiffDay(effectTime = "") {
const strArr = effectTime.split("-");
const currentDay = new Date(effectTime).getDate();
const totalDay = new Date(strArr[0], strArr[1], "0");
return totalDay.getDate() - currentDay + 1;
}
export function toFixed(num) {
let str = num.toFixed(3);
return str.substring(0, str.length - 1);
}
export function diffDayTotal(start, end) {
const d1 = new Date(start + " 00:00:00");
const d2 = new Date(end + " 23:59:59");
return parseInt((d2 - d1 + 1000) / (1000 * 60 * 60 * 24));
}
function getDiffMonth(start, end) {
//相差多少个月
const dt1 = start.split("-");
const dt2 = end.split("-");
return dt2[0] * 12 + dt2[1] * 1 - (dt1[0] * 12 + dt1[1] * 1);
}
function getDiffDay(start, end) { //相差多少天
const dt1 = new Date(start.replace(/-/g, "/"), " 00:00:00");
const dt2 = new Date(end.replace(/-/g, "/"), " 23:59:59");
return (dt2 - dt1 + 1000) / (60 * 60 * 24 * 1000);
}
export function getMonthEndDay(year: number, month: number): number {
return 32 - new Date(year, month - 1, 32).getDate();
}
export function getMonthEndDay(year: number, month: number): number {
return new Date(year, month +1, -1).getDate();
}
export function isNumeric(val: string): boolean {
return /^\d+(\.\d+)?$/.test(val);
}
function dateFormat(date, format = 'yyyy-MM-dd') {
if (date) {
var o = {
"M+": date.getMonth() + 1,
"d+": date.getDate(),
"h+": date.getHours(),
"m+": date.getMinutes(),
"s+": date.getSeconds(),
"q+": Math.floor((date.getMonth() + 3) / 3),
"S": date.getMilliseconds()
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
} else {
return "";
}
}
常用时间处理工具js
最新推荐文章于 2024-06-01 21:31:37 发布