运维小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const formatTime = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  9. }
  10. const formatNumber = n => {
  11. n = n.toString()
  12. return n[1] ? n : `0${n}`
  13. }
  14. const Background_base64 = path => {
  15. return 'data:image/png;base64,' + wx.getFileSystemManager().readFileSync(path, 'base64');
  16. }
  17. // 时间戳转时间(带补零)
  18. const timestamp = n => {
  19. const date = new Date(n);
  20. const year = date.getFullYear();
  21. const month = padZero(date.getMonth() + 1); // 月份补零
  22. const day = padZero(date.getDate()); // 日期补零
  23. const hours = padZero(date.getHours()); // 小时补零
  24. const minutes = padZero(date.getMinutes()); // 分钟补零
  25. const seconds = padZero(date.getSeconds()); // 秒数补零
  26. // 补零函数
  27. function padZero(num) {
  28. return num < 10 ? '0' + num : num;
  29. }
  30. return `${year}-${month}-${day}`;
  31. }
  32. // 补零工具函数(公共复用)
  33. const padZero = num => num < 10 ? '0' + num : num;
  34. // 时间转时间戳(支持YYYY-MM-DD、YYYY-MM-DD HH:MM:SS等格式)
  35. const dateToTimestamp = dateStr => {
  36. if (!dateStr) {
  37. console.error('请传入有效的日期字符串');
  38. return null;
  39. }
  40. // 处理中文格式(如“2023年10月05日”)转为标准格式
  41. const normalized = dateStr
  42. .replace(/年|月/g, '-')
  43. .replace(/日/g, ' ')
  44. .trim();
  45. const timestamp = new Date(normalized).getTime();
  46. if (isNaN(timestamp)) {
  47. console.error('无效的日期格式,请使用YYYY-MM-DD或YYYY-MM-DD HH:MM:SS');
  48. return null;
  49. }
  50. return timestamp;
  51. };
  52. // 封装获取当前时间(自动补零)
  53. const getCurrentTime = () => {
  54. const date = new Date();
  55. // 年/月/日/时/分/秒 自动补零(个位数补0成两位数)
  56. const year = date.getFullYear();
  57. const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份从0开始,需+1
  58. const day = date.getDate().toString().padStart(2, '0');
  59. const hour = date.getHours().toString().padStart(2, '0');
  60. const minute = date.getMinutes().toString().padStart(2, '0');
  61. const second = date.getSeconds().toString().padStart(2, '0');
  62. // 返回多种格式,按需选择
  63. return {
  64. ymd: `${year}-${month}-${day}`, // 2025-08-05
  65. hms: `${hour}:${minute}:${second}`, // 14:03:09
  66. ymdhms: `${year}-${month}-${day} ${hour}:${minute}:${second}` // 2025-08-05 14:03:09
  67. };
  68. }
  69. // 计算昨天的日期(返回 YYYY-MM-DD 格式)
  70. const getYesterday = () => {
  71. const today = new Date();
  72. // 减去一天的毫秒数(24*60*60*1000)
  73. const yesterday = new Date(today.getTime() - 24 * 60 * 60 * 1000);
  74. const year = yesterday.getFullYear();
  75. // 月份从 0 开始,需 +1
  76. const month = String(yesterday.getMonth() + 1).padStart(2, '0');
  77. const day = String(yesterday.getDate()).padStart(2, '0');
  78. return `${year}-${month}-${day}`;
  79. }
  80. module.exports = {
  81. formatTime,
  82. Background_base64,
  83. timestamp,
  84. getCurrentTime,
  85. dateToTimestamp,
  86. getYesterday
  87. }