当前位置: 首页 > news >正文

自制 js 的 VB 风格日期时间处理函数

还是用不惯 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博客

http://www.zskr.cn/news/1495141.html

相关文章:

  • 如何用Python构建个人数字图书馆:fanqie-novel-download终极指南
  • MATLAB手写汉字识别工具包:含训练模型、预处理脚本与可交互GUI界面
  • 长沙AI精准获客公司排行:合规与效果双维度实测 - 起跑123
  • 别再让数据裸奔了!手把手教你为HDFS 3.x配置透明加密与KMS(附避坑指南)
  • 2026中山市家里卫生间漏水、阳台漏水、楼顶漏水、阳台漏水、地下室渗水、阳光房漏水各种房屋漏水情况不用愁!本地防水补漏公司为您排忧解难!您附近的专业防水团队 - 企业资讯
  • 【Springboot毕设全套源码+文档】基于Spring Boot的人力资源数据分析设计与实现(丰富项目+远程调试+讲解+定制)
  • 2026惠州市家里卫生间漏水、阳台漏水、楼顶漏水、阳台漏水、地下室渗水、阳光房漏水各种房屋漏水情况不用愁!本地防水补漏公司为您排忧解难!您附近的专业防水团队 - 企业资讯
  • 2026荆门市家里卫生间漏水、阳台漏水、楼顶漏水、阳台漏水、地下室渗水、阳光房漏水各种房屋漏水情况不用愁!本地防水补漏公司为您排忧解难!您附近的专业防水团队 - 企业资讯
  • 当代码编辑器遇见投资助手:韭菜盒子的神奇融合之旅
  • 如何轻松生成Beyond Compare 5密钥:小白也能懂的完整激活指南
  • spring一个错误修正
  • 2026桂林市家里卫生间漏水、阳台漏水、楼顶漏水、阳台漏水、地下室渗水、阳光房漏水各种房屋漏水情况不用愁!本地防水补漏公司为您排忧解难!您附近的专业防水团队 - 企业资讯
  • 2026东营市家里卫生间漏水、阳台漏水、楼顶漏水、阳台漏水、地下室渗水、阳光房漏水各种房屋漏水情况不用愁!本地防水补漏公司为您排忧解难!您附近的专业防水团队 - 企业资讯
  • Django后端+Vue前端的完整订餐系统毕业设计资源:含可运行代码、MySQL数据库、论文材料与实操视频
  • 告别龟速下载!BaiduPCS-Web:百度网盘免费加速解决方案终极指南
  • 别再踩坑了!CAPL脚本里变量作用域和static的坑,我帮你总结好了
  • 2026防城港市家里卫生间漏水、阳台漏水、楼顶漏水、阳台漏水、地下室渗水、阳光房漏水各种房屋漏水情况不用愁!本地防水补漏公司为您排忧解难!您附近的专业防水团队 - 企业资讯
  • AI系统的数据隐私:一个被严重简化的命题
  • 模电数电期末复习别慌!手把手教你用Multisim仿真搞定戴维南定理和卡诺图
  • 2026那面服饰十大品牌实力榜:六家高潜力国产设计师品牌的版型创新与口碑深度解析 - 品牌发掘
  • 如何3分钟完成LXMusic音源配置:全网音乐一站式解决方案终极指南
  • 三步快速备份你的QQ空间青春记忆:GetQzonehistory完整指南
  • 2026年6月9日四川地区镀锌钢管现货库存;友发,正大,华岐,振鸿正在预售 - 四川盛世钢联营销中心
  • Python 爬虫实战:问答平台问题与答案数据采集
  • Halcon亚像素测量实战:从edges_sub_pix到fit_circle_contour_xld的完整避坑指南
  • 从“梯度消失”到“恒等映射”:用大白话和代码图解ResNet的Shortcut为什么能救活超深网络
  • 2026年10款主流论文降AI率软件推荐
  • 告别调参玄学:用Halcon灰度共生矩阵(GLCM)与频域滤波实战工业缺陷检测
  • 3分钟快速修复损坏视频:untrunc开源视频修复工具终极指南
  • 南昌市GEO AI优化技术领先的服务商推荐 - 舒雯文化