还是用不惯 js 的日期时间处理函数感觉太倮、太猡了所以按照 VBS 使用习惯自制了一批人话版的日期处理函数经常要用到// // 人话版 日期函数 (彻底告别JS反人类命名) // // 字符串转时间对象 function date(d) { if(IsDate(d)){return new Date(d);}else{return Now();} } // 当前时间对象 vb6的 Now() function Now() { return new Date(); } // 当前时间戳毫秒 function TMsc() { return new Date().getTime(); } // 判断是否为有效日期复刻 VB IsDate() function IsDate(d) { // 空值直接返回 false if (!d) return false; // 转成日期并判断是否有效 dt instanceof Date let dt new Date(d); return !isNaN(dt.getTime()); } // 返回时间戳毫秒需要时再用 function TimeS(d) { if(IsDate(d)){return new Date(d).getTime();}else{return TMsc();} } // 取 年 function Year(d) { if (IsDate(d)) { return d.getFullYear(); } else { return Now().getFullYear(); } } // 取 月 function Month(d) { if (IsDate(d)) { return d.getMonth() 1; // JS 月份从0开始1 对齐 VB } else { return Now().getMonth() 1; } } // 获取 几号1~31 function Day(d) { if(IsDate(d)){return d.getDate();}else{return Now().getDate();} } // 获取 星期几0~6 function WeekDay(d) { if(IsDate(d)){return d.getDay()||7;}else{return Now().getDay()||7;} } // 取 小时 function Hour(d) { if (IsDate(d)) { return d.getHours(); } else { return Now().getHours(); } } // 取 分钟 function Minute(d) { if (IsDate(d)) { return d.getMinutes(); } else { return Now().getMinutes(); } } // 取 秒 function Second(d) { if (IsDate(d)) { return d.getSeconds(); } else { return Now().getSeconds(); } } // 取 毫秒 function Sms(d) { if (IsDate(d)) { return d.getMilliseconds(); } else { return Now().getMilliseconds(); } } // // DayOfYear(d) // 功能返回日期是 【年内第几天】和 VB6 完全一致 // function DayOfYear(d) { if (!IsDate(d)) d Now(); let start new Date(d.getFullYear(), 0, 0); let diff d - start; let oneDay 1000 * 60 * 60 * 24; return Math.floor(diff / oneDay); } // // WeekOfYear(d) // 功能返回日期是 【年内第几周】ISO 标准最常用 // function WeekOfYear(d) { if (!IsDate(d)) d Now(); let dt new Date(d); dt.setHours(0, 0, 0, 0); dt.setDate(dt.getDate() 4 - (dt.getDay() || 7)); let yearStart new Date(dt.getFullYear(), 0, 1); let weekNo Math.ceil((((dt - yearStart) / 86400000) 1) / 7); return weekNo; } // // VB6 风格 DateAdd (支持 周) /* y 年 m 月 w 周 d 日 h 时 n 分 s 秒 ms 毫秒 */ // function DateAdd(interval, number, date) { let dt IsDate(date) ? new Date(date) : Now(); number Number(number) || 0; switch (interval.toLowerCase()) { case y: case yyyy: case year: dt.setFullYear(dt.getFullYear() number); break; case m: case month: dt.setMonth(dt.getMonth() number); break; case d: case day: dt.setDate(dt.getDate() number); break; case w: case week: dt.setDate(dt.getDate() number * 7); break; // 加周 case h: case hour: dt.setHours(dt.getHours() number); break; case n: case minute: dt.setMinutes(dt.getMinutes() number); break; case s: case second: dt.setSeconds(dt.getSeconds() number); break; case ms: case millisecond: dt.setMilliseconds(dt.getMilliseconds() number); break; } return dt; } // // VB6 风格 DateDiff (支持 周) // function DateDiff(interval, date1, date2) { let dt1 IsDate(date1) ? new Date(date1) : Now(); let dt2 IsDate(date2) ? new Date(date2) : Now(); let msDiff dt2.getTime() - dt1.getTime(); // 关键统一归零到当天 00:00:00.000 let d1 new Date(dt1.getFullYear(), dt1.getMonth(), dt1.getDate()); let d2 new Date(dt2.getFullYear(), dt2.getMonth(), dt2.getDate()); let dayMsDiff d2 - d1; // 纯日期毫秒差一定是86400000的整数倍 switch (interval.toLowerCase()) { case y: case yyyy: case year: return Year(dt2) - Year(dt1); case m: case month: return (Year(dt2) - Year(dt1)) * 12 (Month(dt2) - Month(dt1)); case d: case day: return dayMsDiff / 86400000; // ✅ 直接除不用floor case w: case week: return (dayMsDiff / 86400000) / 7; // 小时、分钟、秒 归零处理 case h: case hour:// 归零到整点去掉分钟、秒、毫秒 let h1 new Date(dt1.getFullYear(), dt1.getMonth(), dt1.getDate(), dt1.getHours(), 0, 0, 0); let h2 new Date(dt2.getFullYear(), dt2.getMonth(), dt2.getDate(), dt2.getHours(), 0, 0, 0); return (h2 - h1) / 3600000; case n: case minute:// 归零到整分去掉秒、毫秒 let n1 new Date(dt1.getFullYear(), dt1.getMonth(), dt1.getDate(), dt1.getHours(), dt1.getMinutes(), 0, 0); let n2 new Date(dt2.getFullYear(), dt2.getMonth(), dt2.getDate(), dt2.getHours(), dt2.getMinutes(), 0, 0); return (n2 - n1) / 60000; case s: case second:// 归零到整秒去掉毫秒 let s1 new Date(dt1.getFullYear(), dt1.getMonth(), dt1.getDate(), dt1.getHours(), dt1.getMinutes(), dt1.getSeconds(), 0); let s2 new Date(dt2.getFullYear(), dt2.getMonth(), dt2.getDate(), dt2.getHours(), dt2.getMinutes(), dt2.getSeconds(), 0); return (s2 - s1) / 1000; case ms: case millisecond: return msDiff; } return 0; } // // JS版 VBSformatDT (1:1复刻VBS函数) // 所有typex编号、输出格式和你原来的完全一样 // 全部用你自制的VB风格日期函数 // function VBSformatDT(dt, typex) { // 自动容错无效日期用当前时间 if (!IsDate(dt)) dt Now(); switch (typex) { case 10: return Str(Year(dt)) - Right(0 Str(Month(dt)), 2) - Right(0 Str(Day(dt)), 2) Right(0 Str(Hour(dt)), 2) :00:00; case 11: return Str(Year(dt)) - Right(0 Str(Month(dt)), 2) - Right(0 Str(Day(dt)), 2); case 14: return Str(Year(dt)) - Right(0 Str(Month(dt)), 2) - Right(0 Str(Day(dt)), 2) Right(0 Str(Hour(dt)), 2) : Right(0 Str(Minute(dt)), 2) : Right(0 Str(Second(dt)), 2); case 15: return Str(Year(dt)) - Right(0 Str(Month(dt)), 2) - Right(0 Str(Day(dt)), 2) Right(0 Str(Hour(dt)), 2) : Right(0 Str(Minute(dt)), 2) : Right(0 Str(Second(dt)), 2) . Right(00 Str(Sms(dt)), 3); case 16: return Str(Year(dt)) - Right(0 Str(Month(dt)), 2); case 17: return Right(0 Str(Month(dt)), 2) - Right(0 Str(Day(dt)), 2); case 21: return Str(Year(dt)) - Str(Month(dt)) - Str(Day(dt)); case 24: return Str(Year(dt)) - Str(Month(dt)) - Str(Day(dt)) Str(Hour(dt)) : Right(0 Str(Minute(dt)), 2) : Right(0 Str(Second(dt)), 2); case 30: return Right(0 Str(Hour(dt)), 2) :00; case 31: return Str(Year(dt)) 年 Str(Month(dt)) 月 Str(Day(dt)) 日; case 32: return Str(Year(dt)) 年 Str(Month(dt)) 月 Str(Day(dt)) 日 Right(0 Str(Hour(dt)), 2) 时; case 34: return Str(Year(dt)) 年 Right(0 Str(Month(dt)), 2) 月 Right(0 Str(Day(dt)), 2) 日 Right(0 Str(Hour(dt)), 2) : Right(0 Str(Minute(dt)), 2) : Right(0 Str(Second(dt)), 2); case 38: return Str(Month(dt)) 月 Str(Day(dt)) 日; case 39: return Str(Month(dt)) 月 Str(Day(dt)) 日 Right(0 Str(Hour(dt)), 2) 时; case 91: return Str(Year(dt)) Right(0 Str(Month(dt)), 2) Right(0 Str(Day(dt)), 2); case 92: return Str(Year(dt)) Right(0 Str(Month(dt)), 2) Right(0 Str(Day(dt)), 2) Right(0 Str(Hour(dt)), 2); case 93: return Str(Year(dt)) Right(0 Str(Month(dt)), 2) Right(0 Str(Day(dt)), 2) Right(0 Str(Hour(dt)), 2) Right(0 Str(Minute(dt)), 2); case 94: return Str(Year(dt)) Right(0 Str(Month(dt)), 2) Right(0 Str(Day(dt)), 2) Right(0 Str(Hour(dt)), 2) Right(0 Str(Minute(dt)), 2) Right(0 Str(Second(dt)), 2); case 95: return Str(Year(dt)) Right(0 Str(Month(dt)), 2) Right(0 Str(Day(dt)), 2) Right(0 Str(Hour(dt)), 2) Right(0 Str(Minute(dt)), 2) Right(0 Str(Second(dt)), 2) Right(00 Str(Sms(dt)), 3); default: return Str(Year(dt)) 年 Str(Month(dt)) 月 Str(Day(dt)) 日 Str(Hour(dt)) : Right(0 Str(Minute(dt)), 2) : Right(0 Str(Second(dt)), 2); } }里面涉及的自定义字符串处理函数参见 自制 js 的 trim、right、left、instrRev、instr、mid、chr、asc 函数_js instr-CSDN博客