function dateFormat(date, format) {
if (!(date instanceof Date)) {
date = new Date(date);
}
if (date.toString() === "Invalid Date") {
return '';
}
const formatter = {
'Y': () => date.getFullYear(),
'm': () => padLeftZero(date.getMonth() + 1),
'd': () => padLeftZero(date.getDate()),
'H': () => padLeftZero(date.getHours()),
'i': () => padLeftZero(date.getMinutes()),
's': () => padLeftZero(date.getSeconds()),
'v': () => padLeftZero(date.getMilliseconds())
};
for (let key in formatter) {
if (formatter.hasOwnProperty(key)) {
console.log((formatter[key]).call(date, date))
format = replaceAll(format, key, (formatter[key]).call(date, date));
console.log(1,format)
}
}
return format;
}
function replaceAll(str, search, replace) {
if (!isString(str) || str.length < 1 || !isString(search) || search.length < 1) {
return str;
}
let text = str;
while (text.indexOf(search) !== -1) {
text = text.replace(search, replace);
}
return text;
}
function isString(val) {
return typeof val === "string";
}
function padLeftZero(str, maxLength = 2) {
return padLeft(str, maxLength, '0');
}
function padLeft(str, maxLength = 2, padStr = ' ') {
return (str + '').padStart(maxLength, padStr);
}
var a = "" + dateFormat(new Date(), "YmdHisv") + Math.floor(Math.random() * 1000);
console.log(a)
console