ipink-util
Version:
util.js
323 lines (320 loc) • 11 kB
JavaScript
import { isDate, isNumber, isString, isNaN } from './is.mjs';
const parseDate = (target) => {
if (isDate(target)) return target;
if (isNumber(target)) return new Date(target);
if (isString(target)) {
const timestamp = Date.parse(target);
if (isNaN(timestamp)) return null;
return new Date(timestamp);
}
return null;
};
const getDate = (time, type, s) => {
s = s || "/";
let date = /* @__PURE__ */ new Date();
if (isNumber(time)) date = new Date(time);
else if (isString(time)) {
const _time = time;
if (_time.indexOf("-") > -1) date = new Date(_time.split("-").join("/"));
else date = new Date(time);
} else if (isDate(time)) date = time;
let year = "" + date.getFullYear();
let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1 + "";
let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate() + "";
let hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours() + "";
let minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes() + "";
let second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds() + "";
let milliSecond = date.getMilliseconds();
let currentTime = year + s + month + s + day + " " + hour + ":" + minute + ":" + second;
if (type == 1) return year + s + month + s + day;
else if (type == 2) return hour + ":" + minute;
else if (type == 3) return year + s + month;
else if (type == 4) return year;
else if (type == 5) return month + s + day;
else if (type == -1) {
return {
currentTime,
year,
month,
day,
hour,
minute,
second,
milliSecond
};
} else return currentTime;
};
const getRecentWeek = (weekCount = 0, split = "/") => {
const date = /* @__PURE__ */ new Date();
let totalDayCount = weekCount * 7;
let startDayCount = 7;
let endDayCount = 13;
let weekDate = new Date(date.getTime() - 7 * 24 * 3600 * 1e3);
let weekDate2 = new Date(date.getTime() - 7 * 24 * 3600 * 1e3);
let day = weekDate.getDay();
let time = weekDate.getDate() - day + (day === 0 ? -6 : 1);
let startDate = new Date(weekDate.setDate(time + (startDayCount - totalDayCount)));
let endDate = new Date(weekDate2.setDate(time + (endDayCount - totalDayCount)));
let startDay = startDate.getDate();
let startMonth = startDate.getMonth() + 1;
let endDay = endDate.getDate();
let endMonth = endDate.getMonth() + 1;
let start = startDate.getFullYear() + split + (startMonth < 10 ? "0" + startMonth : startMonth) + split + (startDay < 10 ? "0" + startDay : startDay) + " 00:00:00";
const end = endDate.getFullYear() + split + (endMonth < 10 ? "0" + endMonth : endMonth) + split + (endDay < 10 ? "0" + endDay : endDay) + " 23:59:59";
return [start, end];
};
const getRecentMonth = (monthCount = 0, split = "/", targetDate) => {
if (isString(targetDate)) targetDate = targetDate.split("-").join("/");
const date = targetDate ? new Date(targetDate) : /* @__PURE__ */ new Date();
date.setMonth(date.getMonth() - monthCount);
let y = date.getFullYear();
let m = date.getMonth() + 1;
date.setMonth(date.getMonth() + 1);
date.setDate(0);
let d = date.getDate();
m = m < 10 ? "0" + m : m;
d = d < 10 ? "0" + d : d;
let start = y + split + m + split + "01 00:00:00";
let end = y + split + m + split + d + " 23:59:59";
return [start, end];
};
const getRecentYear = (yearCount = 0, split = "/", targetDate) => {
if (isString(targetDate)) targetDate = targetDate.split("-").join("/");
const date = targetDate ? new Date(targetDate) : /* @__PURE__ */ new Date();
let y = date.getFullYear() - yearCount;
date.setMonth(12);
date.setDate(0);
let d = date.getDate();
let start = y + split + "01" + split + "01 00:00:00";
let end = y + split + "12" + split + d + " 23:59:59";
return [start, end];
};
const getRecentDay = (dayCount = 1, split = "/", targetDate) => {
if (isString(targetDate)) targetDate = targetDate.split("-").join("/");
var d = targetDate ? new Date(targetDate) : /* @__PURE__ */ new Date();
d.setDate(d.getDate() + dayCount);
let yy = d.getFullYear();
let mm = d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1;
let dd = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();
let day = yy + split + mm + split + dd;
let today = getDate(void 0, 1);
return [day, today].sort();
};
const getSomeday = (day, split = "/") => {
if (isString(day)) day = day.split("-").join("/");
let today = new Date(day);
let tYear = today.getFullYear();
let tMonth = formatNumber(today.getMonth() + 1);
let tDate = formatNumber(today.getDate());
let tWeek = today.getDay();
let tTime = parseInt("" + today.getTime() / 1e3);
let hour = formatNumber(today.getHours());
let minute = formatNumber(today.getMinutes());
let second = formatNumber(today.getSeconds());
let milliSecond = today.getMilliseconds();
const week = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
function formatNumber(n) {
n = n.toString();
return n[1] ? n : "0" + n;
}
return {
"time": tTime,
// 时间戳
"year": tYear,
"month": tMonth,
"day": tDate,
hour,
minute,
second,
milliSecond,
"week": week[tWeek],
"md": tMonth + split + tDate,
"ymd": tYear + split + tMonth + split + tDate,
"currentTime": tYear + split + tMonth + split + tDate + " " + hour + ":" + minute + ":" + second
};
};
const getDateScoped = (type, sign = "/", count = -1) => {
let _now = getDate(void 0, 1, sign);
let end = _now + " 23:59:49", timestamp = Date.now(), date = /* @__PURE__ */ new Date(), start = _now + " 00:00:00";
if (count > 0) {
start = getDate(timestamp - 1e3 * 60 * 60 * 24 * count, 1, sign) + " 00:00:00";
} else {
if (type == 1) {
const [_start, _end] = getRecentWeek(0, sign);
start = _start;
end = _end;
} else if (type == 2) {
const [_start, _end] = getRecentWeek(1, sign);
start = _start;
end = _end;
} else if (type == 3) {
let month = date.getMonth() + 1;
if (month < 10) {
month = "0" + month;
}
let year2 = date.getFullYear();
start = year2 + sign + month + sign + "01 00:00:00";
} else if (type == 4) {
let year2 = date.getFullYear();
let month = date.getMonth();
if (month === 0) {
month = 12;
year2 = year2 - 1;
} else if (month < 10) {
month = "0" + month;
}
let monthDate = new Date(year2, +month, 0);
start = year2 + sign + month + sign + "01 00:00:00";
end = year2 + sign + month + sign + monthDate.getDate() + " 23:59:59";
} else if (type == 5) {
var year = date.getFullYear();
start = year + sign + "01" + sign + "01 00:00:00";
end = year + sign + "12" + sign + "31 23:59:59";
} else if (type == 6) {
var year = date.getFullYear() - 1;
start = year + sign + "01" + sign + "01 00:00:00";
end = year + sign + "12" + sign + "31 23:59:59";
}
}
return [start, end];
};
const getDaysBetween = (target, current) => {
if (isString(target)) target = target.split("-").join("/");
if (current && isString(current)) current = current.split("-").join("/");
let startDate;
if (Array.isArray(target)) target = target[0];
if (typeof target == "string" && target.indexOf(" ") > -1) target = target.split(" ")[0];
if (!target) return 0;
try {
current = getDate(current, 1);
startDate = (/* @__PURE__ */ new Date(current + " 00:00:00")).getTime();
let endDate = (/* @__PURE__ */ new Date(target + " 00:00:00")).getTime();
if (startDate == endDate) return 0;
let days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1e3);
return days;
} catch (e) {
return 0;
}
};
const getWeek = (date, lang) => {
var day;
if (typeof date == "number" && date < 7) day = date;
else {
if (isString(date)) date = date.split("-").join("/");
day = new Date(date).getDay();
}
lang = lang || "zh-Hant";
const weekLang = /* @__PURE__ */ new Map([
[0, "Sun"],
[1, "Mon"],
[2, "Tue"],
[3, "Wed"],
[4, "Thu"],
[5, "Fri"],
[6, "Sat"]
]);
var weeks = new Array(
"周日",
"周一",
"周二",
"周三",
"周四",
"周五",
"周六"
);
var week = lang.startsWith("zh-Han") ? weeks[day] : weekLang.get(day);
return week;
};
const transformDate = (options) => {
let { date, type = 4, sign = "/", lang } = options;
if (!date) return "";
if (isString(date)) {
const hasSign = date.indexOf("-") > -1;
date = date.split(hasSign ? "-" : "/").join(sign);
} else date = getDate(date, 1, sign);
lang = lang || "zh-Han";
const monthLang = /* @__PURE__ */ new Map([
[1, "Jan."],
[2, "Feb."],
[3, "Mar."],
[4, "Apr."],
[5, "May."],
[6, "Jun."],
[7, "Jul."],
[8, "Aug."],
[9, "Sept."],
[10, "Oct."],
[11, "Nov."],
[12, "Dec."]
]);
const dayLang = /* @__PURE__ */ new Map([
[1, "1st"],
[2, "2nd"],
[3, "3rd"],
[4, "4th"],
[5, "5th"],
[6, " 6th"],
[7, "7th"],
[8, "8th"],
[9, "9th"],
[10, "10th"],
[11, "11th"],
[12, "12th"],
[13, "13th"],
[14, "14th"],
[15, "15th"],
[16, "16th"],
[17, "17th"],
[18, "18th"],
[19, "19th"],
[20, "20th"],
[21, "21st"],
[22, "22nd"],
[23, "23rd"],
[24, "24th"],
[25, "25th"],
[26, "26th"],
[27, "27th"],
[28, "28th"],
[29, "29th"],
[30, "30th"],
[31, "31st"]
]);
let str = "";
let strArr = date.split(sign).map((item) => Number(item));
try {
if (!type) {
str = lang.startsWith("zh-Han") ? strArr[0] + "年" + strArr[1] + "月" + strArr[2] + "日" : monthLang.get(strArr[1]) + " " + strArr[2] + ", " + strArr[0];
}
if (type == 0.5) {
str = lang.startsWith("zh-Han") ? strArr[0] + "年" + strArr[1] + "月" : monthLang.get(strArr[1]) + " " + strArr[0];
}
if (type == 1) {
str = lang.startsWith("zh-Han") ? strArr[0] + "年" : strArr[0] + "";
}
if (type == 2) {
str = lang.startsWith("zh-Han") ? strArr[1] + "月" : monthLang.get(strArr[1]) + "";
}
if (type == 3) {
str = lang.startsWith("zh-Han") ? strArr[2] + "日" : dayLang.get(strArr[2]) + "";
}
if (type == 4) {
str = lang.startsWith("zh-Han") ? strArr[1] + "月" + strArr[2] + "日" : monthLang.get(strArr[1]) + " " + strArr[2];
}
if (type == 5) {
str = getWeek(date, lang);
}
} catch (e) {
str = date;
}
return str || date;
};
const formatDate = (year, month, day, sign = "/") => {
let y = year;
let m = month;
if (m && m < 10) m = `0${m}`;
let d = day;
if (d && d < 10) d = `0${d}`;
return (y ? y + sign : "") + m + (d ? sign + d : "");
};
export { formatDate, getDate, getDateScoped, getDaysBetween, getRecentDay, getRecentMonth, getRecentWeek, getRecentYear, getSomeday, getWeek, parseDate, transformDate };