You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
2.3 KiB

2 years ago
// 2022-08-31T09:45:51.9340433 转 2022-08-31 09:45:51
export function formatTimeStrToStr(timeStr) {
if (timeStr.lastIndexOf('.') == -1) {
return timeStr.replace('T',' ').substring(0,timeStr.length)
}
return timeStr.replace('T',' ').substring(0,timeStr.lastIndexOf('.'))
}
// 2022-08-31 09:45:51 转 2022-08-31T09:45:51
export function formatTimeToT(timeStr) {
if(!timeStr) return ""
let _str = timeStr.replace(/(^\s*)|(\s*$)/g, "");
return _str.replace(' ','T').substring(0,_str.length)
}
// 时间转化格式化,使用位置(供应导入,excel中时间转义)
/**
     * @param {*} data 当前时间
     * @param {*} type date/默认返回日期   time返回时间   datetime日期+时间
     * @param {*} connect 转义链接符默认-只适合日期连接符
     *                    如果不是zh则链接统一为传入值zh则为年月日时分秒
     *                    如果类型为数组则直接为数组,也适合时间连接符有效数组格式为3位或6位
     * @returns String
     */
export function formatTimeStampToNorm(data,type,connect){
if(!data || data.length <= 0){return ""}
let _date = new Date(data)
if(!type) type = "date";
if(!connect) connect = "-";
let year = __formatLength(_date.getFullYear());
let month = __formatLength(_date.getMonth() + 1);
let day = __formatLength(_date.getDate());
let hour = __formatLength(_date.getHours());
let minute = __formatLength(_date.getMinutes());
let second = __formatLength(_date.getSeconds());
function __formatLength(value){
if(Number(value) < 10){
return "0" + value
}else{
return value
}
}
let arr = (connect == 'zh') ? ["年","月","日","时","分","秒"] : [connect,connect,"",":",":",""]
if(typeof connect == "Array") arr = connect;
switch(type){
case "date":
return year+arr[0] + month+arr[1] + day+arr[2];
case "time":
return hour+arr[3] + minute+arr[4] + second+arr[5];
default:
return year+arr[0] + month+arr[1] + day+arr[2] + " " +hour+arr[3] + minute+arr[4] + second+arr[5];
}
}