nmgwap-time-formatting
Version:
时间格式化库
188 lines (178 loc) • 4.96 kB
JavaScript
/**
* @description 获取年
* @author 青年码农
* @param? 时间
* @returns yyyy
*/
export const getY = (date = "") => {
const dt = date == "" ? new Date() : new Date(date);
if (dt == "Invalid Date") {
return `格式错误`;
}
const y = dt.getFullYear();
return `${y}`;
};
/**
* @description 获取月
* @author 青年码农
* @param? 时间
* @returns m
*/
export const getM = (date = "") => {
const dt = date == "" ? new Date() : new Date(date);
if (dt == "Invalid Date") {
return `格式错误`;
}
const m = (dt.getMonth() + 1 + "").padStart(2, "0");
return `${m}`;
};
/**
* @description 获取日
* @author 青年码农
* @param? 时间
* @returns d
*/
export const getD = (date = "") => {
const dt = date == "" ? new Date() : new Date(date);
if (dt == "Invalid Date") {
return `格式错误`;
}
const d = (dt.getDate() + "").padStart(2, "0");
return `${d}`;
};
/**
* @description 获取年月日
* @author 青年码农
* @param? 时间
* @returns yyyy-mm-dd
*/
export const getYMD = (date = "", connector = "-") => {
const dt = date == "" ? new Date() : new Date(date);
if (dt == "Invalid Date") {
return `格式错误`;
}
const y = dt.getFullYear();
const m = (dt.getMonth() + 1 + "").padStart(2, "0");
const d = (dt.getDate() + "").padStart(2, "0");
if (connector == "HZ") {
return `${y}年${m}月${d}日`;
} else {
return `${y}${connector}${m}${connector}${d}`;
}
};
/**
* @description 获取月日
* @author 青年码农
* @param? 时间
* @returns mm-dd
*/
export const getMD = (date = "", connector = "-") => {
const dt = date == "" ? new Date() : new Date(date);
if (dt == "Invalid Date") {
return `格式错误`;
}
const m = (dt.getMonth() + 1 + "").padStart(2, "0");
const d = (dt.getDate() + "").padStart(2, "0");
if (connector == "HZ") {
return `${m}月${d}日`;
} else {
return `${m}${connector}${d}`;
}
};
/**
* @description 获取年月日时分
* @author 青年码农
* @param? 时间
* @returns yyyy-mm-dd hh:mm
*/
export const getYMDHM = (date = "", connector = "-") => {
const dt = date == "" ? new Date() : new Date(date);
if (dt == "Invalid Date") {
return `格式错误`;
}
const y = dt.getFullYear();
const m = (dt.getMonth() + 1 + "").padStart(2, "0");
const d = (dt.getDate() + "").padStart(2, "0");
const hh = (dt.getHours() + "").padStart(2, "0");
const mm = (dt.getMinutes() + "").padStart(2, "0");
if (connector == "HZ") {
return `${y}年${m}月${d}日${hh}时${mm}分`;
} else {
return `${y}${connector}${m}${connector}${d} ${hh}:${mm}`;
}
};
/**
* @description 获取年月日时分秒
* @author 青年码农
* @param? 时间
* @returns yyyy-mm-dd hh:mm:ss
*/
export const getYMDHMS = (date = "", connector = "-") => {
const dt = date == "" ? new Date() : new Date(date);
if (dt == "Invalid Date") {
return `格式错误`;
}
const y = dt.getFullYear();
const m = (dt.getMonth() + 1 + "").padStart(2, "0");
const d = (dt.getDate() + "").padStart(2, "0");
const hh = (dt.getHours() + "").padStart(2, "0");
const mm = (dt.getMinutes() + "").padStart(2, "0");
const ss = (dt.getSeconds() + "").padStart(2, "0");
if (connector == "HZ") {
return `${y}年${m}月${d}日${hh}时${mm}分${ss}秒`;
} else {
return `${y}${connector}${m}${connector}${d} ${hh}:${mm}:${ss}`;
}
};
/**
* @description 获取时分
* @author 青年码农
* @param? 时间
* @returns hh:mm
*/
export const getHM = (date = "", connector = ":") => {
const dt = date == "" ? new Date() : new Date(date);
if (dt == "Invalid Date") {
return `格式错误`;
}
const hh = (dt.getHours() + "").padStart(2, "0");
const mm = (dt.getMinutes() + "").padStart(2, "0");
if (connector == "HZ") {
return `${hh}时${mm}分`;
} else {
return `${hh}${connector}${mm}`;
}
};
/**
* @description 获取时分秒
* @author 青年码农
* @param? 时间
* @returns hh:mm:ss
*/
export const getHMS = (date = "", connector = ":") => {
const dt = date == "" ? new Date() : new Date(date);
if (dt == "Invalid Date") {
return `格式错误`;
}
const hh = (dt.getHours() + "").padStart(2, "0");
const mm = (dt.getMinutes() + "").padStart(2, "0");
const ss = (dt.getSeconds() + "").padStart(2, "0");
if (connector == "HZ") {
return `${hh}时${mm}分${ss}秒`;
} else {
return `${hh}${connector}${mm}${connector}${ss}`;
}
};
/**
* @description 获取时间戳
* @author 青年码农
* @param? 时间
* @returns hh:mm:ss
*/
export const getTimeStamp = (date = "") => {
const dt = date == "" ? new Date() : new Date(date);
if (dt == "Invalid Date") {
return `格式错误`;
}
return dt.getTime();
};