erp-el-element/utils/index.js

201 lines
4.8 KiB
JavaScript
Raw Permalink 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.

/**
* Created by PanJiaChen on 16/11/18.
*/
/**
* Parse the time to string
* @param {(Object|string|number)} time
* @param {string} cFormat
* @returns {string | null}
*/
export function parseTime(time, cFormat) {
if (arguments.length === 0 || !time) {
return null;
}
const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
let date;
if (typeof time === "object") {
date = time;
} else {
if (typeof time === "string") {
if (/^[0-9]+$/.test(time)) {
// support "1548221490638"
time = parseInt(time);
} else {
// support safari
// https://stackoverflow.com/questions/4310953/invalid-date-in-safari
time = time.replace(new RegExp(/-/gm), "/");
}
}
if (typeof time === "number" && time.toString().length === 10) {
time = time * 1000;
}
date = new Date(time);
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay(),
};
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
const value = formatObj[key];
// Note: getDay() returns 0 on Sunday
if (key === "a") {
return ["日", "一", "二", "三", "四", "五", "六"][value];
}
return value.toString().padStart(2, "0");
});
return time_str;
}
/**
* @param {number} time
* @param {string} option
* @returns {string}
*/
export function formatTime(time, option) {
if (("" + time).length === 10) {
time = parseInt(time) * 1000;
} else {
time = +time;
}
const d = new Date(time);
const now = Date.now();
const diff = (now - d) / 1000;
if (diff < 30) {
return "刚刚";
} else if (diff < 3600) {
// less 1 hour
return Math.ceil(diff / 60) + "分钟前";
} else if (diff < 3600 * 24) {
return Math.ceil(diff / 3600) + "小时前";
} else if (diff < 3600 * 24 * 2) {
return "1天前";
}
if (option) {
return parseTime(time, option);
} else {
return (
d.getMonth() +
1 +
"月" +
d.getDate() +
"日" +
d.getHours() +
"时" +
d.getMinutes() +
"分"
);
}
}
/**
* @param {string} url
* @returns {Object}
*/
export function param2Obj(url) {
const search = decodeURIComponent(url.split("?")[1]).replace(/\+/g, " ");
if (!search) {
return {};
}
const obj = {};
const searchArr = search.split("&");
searchArr.forEach((v) => {
const index = v.indexOf("=");
if (index !== -1) {
const name = v.substring(0, index);
const val = v.substring(index + 1, v.length);
obj[name] = val;
}
});
return obj;
}
// 时间戳减去当前时间的天数
export function parseTian(time) {
// 假设你的时间戳为1609459200000
let timestamp = time;
// 将时间戳转换为日期对象
let date = new Date(timestamp);
// 获取当前日期
let now = new Date();
// 计算两个日期之间的毫秒数
let diff = date - now;
// 将毫秒数转换为天数
let days = Math.floor(diff / (1000 * 60 * 60 * 24));
return days;
}
export function calc_day(time) {
let timestamp = time;
// 将时间戳转换为日期对象
let date = new Date(timestamp);
// 获取当前日期
let now = new Date();
// 计算两个日期之间的毫秒数
let diff = now - date;
// 将时间戳相减获得差值(毫秒数)
/**
* @desc 毫秒数除以1000就转为秒数
* @desc 秒数除以60后取整就是分钟因为1分钟等于60秒
* @desc 秒数除以3600后取整就是小时因为1小时等于3600秒
* @desc 小时数除以24后取整就是相差的天数
*/
var day = diff / 1000 / 60 / 60 / 24;
return parseInt(day);
}
//2023-09-18 10:33:45 输出2023-9-18
export function parseDays(time) {
const dateTimeStr = time;
const date = new Date(dateTimeStr);
const year = date.getFullYear();
const month = date.getMonth() + 1; // 注意月份是从0开始的因此需要加1
const day = date.getDate();
const formattedDate = `${year}-${month}-${day}`;
return formattedDate;
}
export function getTimeDays(time) {
var timestamp = time;
var date = new Date(timestamp);
var year = date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).slice(-2); // 注意JavaScript中月份是从0开始的所以我们需要+1
var day = ("0" + date.getDate()).slice(-2);
var hours = ("0" + date.getHours()).slice(-2);
var minutes = ("0" + date.getMinutes()).slice(-2);
var seconds = ("0" + date.getSeconds()).slice(-2);
return year + '-' + month + '-' + day;
// console.log(
// "Date: " +
// year +
// "-" +
// month +
// "-" +
// day +
// " " +
// hours +
// ":" +
// minutes +
// ":" +
// seconds
// );
}