| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- const formatTime = date => {
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- const day = date.getDate()
- const hour = date.getHours()
- const minute = date.getMinutes()
- const second = date.getSeconds()
-
- return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
- }
-
- const formatNumber = n => {
- n = n.toString()
- return n[1] ? n : `0${n}`
- }
- const Background_base64 = path => {
- return 'data:image/png;base64,' + wx.getFileSystemManager().readFileSync(path, 'base64');
-
- }
-
- // 时间戳转时间(带补零)
- const timestamp = n => {
- const date = new Date(n);
-
- const year = date.getFullYear();
- const month = padZero(date.getMonth() + 1); // 月份补零
- const day = padZero(date.getDate()); // 日期补零
- const hours = padZero(date.getHours()); // 小时补零
- const minutes = padZero(date.getMinutes()); // 分钟补零
- const seconds = padZero(date.getSeconds()); // 秒数补零
-
- // 补零函数
- function padZero(num) {
- return num < 10 ? '0' + num : num;
- }
-
- return `${year}-${month}-${day}`;
- }
- // 补零工具函数(公共复用)
- const padZero = num => num < 10 ? '0' + num : num;
-
-
- // 时间转时间戳(支持YYYY-MM-DD、YYYY-MM-DD HH:MM:SS等格式)
- const dateToTimestamp = dateStr => {
- if (!dateStr) {
- console.error('请传入有效的日期字符串');
- return null;
- }
-
- // 处理中文格式(如“2023年10月05日”)转为标准格式
- const normalized = dateStr
- .replace(/年|月/g, '-')
- .replace(/日/g, ' ')
- .trim();
-
- const timestamp = new Date(normalized).getTime();
-
- if (isNaN(timestamp)) {
- console.error('无效的日期格式,请使用YYYY-MM-DD或YYYY-MM-DD HH:MM:SS');
- return null;
- }
-
- return timestamp;
- };
- // 封装获取当前时间(自动补零)
- const getCurrentTime = () => {
- const date = new Date();
- // 年/月/日/时/分/秒 自动补零(个位数补0成两位数)
- const year = date.getFullYear();
- const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份从0开始,需+1
- const day = date.getDate().toString().padStart(2, '0');
- const hour = date.getHours().toString().padStart(2, '0');
- const minute = date.getMinutes().toString().padStart(2, '0');
- const second = date.getSeconds().toString().padStart(2, '0');
-
- // 返回多种格式,按需选择
- return {
- ymd: `${year}-${month}-${day}`, // 2025-08-05
- hms: `${hour}:${minute}:${second}`, // 14:03:09
- ymdhms: `${year}-${month}-${day} ${hour}:${minute}:${second}` // 2025-08-05 14:03:09
- };
- }
- // 计算昨天的日期(返回 YYYY-MM-DD 格式)
- const getYesterday = () => {
- const today = new Date();
- // 减去一天的毫秒数(24*60*60*1000)
- const yesterday = new Date(today.getTime() - 24 * 60 * 60 * 1000);
-
- const year = yesterday.getFullYear();
- // 月份从 0 开始,需 +1
- const month = String(yesterday.getMonth() + 1).padStart(2, '0');
- const day = String(yesterday.getDate()).padStart(2, '0');
-
- return `${year}-${month}-${day}`;
- }
- module.exports = {
- formatTime,
- Background_base64,
- timestamp,
- getCurrentTime,
- dateToTimestamp,
- getYesterday
- }
|