时间函数
parse2Date()
function parse2Date(cellValue) {
if (cellValue === undefined || cellValue === "") {
return "";
}
let date = new Date(Date.parse(cellValue.replace(/-/g, "/")));
return (
date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate()
);
}
date2Str()
function date2Str(date) {
if (date === undefined || !(date instanceof Date)) {
return "";
}
return (
date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate()
);
}
date2TimeStrZero()
function date2TimeStrZero(dateObj) {
if (dateObj === undefined || !(dateObj instanceof Date)) {
return "";
}
return (
dateObj.getFullYear() +
"-" +
String(dateObj.getMonth() + 1).padStart(2, "0") +
"-" +
String(dateObj.getDate()).padStart(2, "0") +
" " +
String(dateObj.getHours()).padStart(2, "0") +
":" +
String(dateObj.getMinutes()).padStart(2, "0") +
":" +
String(dateObj.getSeconds()).padStart(2, "0")
);
}
date2Time()
function date2Time(dateObj) {
if (dateObj === undefined || !(dateObj instanceof Date)) {
return "";
}
return (
String(dateObj.getHours()).padStart(2, "0") +
":" +
String(dateObj.getMinutes()).padStart(2, "0") +
":" +
String(dateObj.getSeconds()).padStart(2, "0")
);
}
formatDate()
function formatDate(value) {
let arr = [];
arr.push(value.getFullYear());
arr.push(
value.getMonth() + 1 < 10
? "0" + (value.getMonth() + 1)
: value.getMonth() + 1
);
arr.push(value.getDate() < 10 ? "0" + value.getDate() : value.getDate());
return arr.join("-");
}
date2Str()
function date2Str(dateObj) {
if (dateObj === undefined || !(dateObj instanceof Date)) {
return "";
}
let str =`${dateObj.getFullYear()}-${dateObj.getMonth()+1}-${dateObj.getDate()}` ;
return str;
}