common.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // 防抖函数
  2. function debounce(func, wait) {
  3. let timeout;
  4. return function (...args) {
  5. clearTimeout(timeout);
  6. timeout = setTimeout(() => func.apply(this, args), wait);
  7. };
  8. }
  9. /**
  10. * 将时间戳转换为字符串格式 YYYY-MM-DD HH:mm:ss
  11. * @param {number} timestamp - 时间戳(毫秒)
  12. * @returns {string} 格式化后的日期字符串
  13. */
  14. function timestampToString(timestamp) {
  15. const date = new Date(timestamp * 1000);
  16. const year = date.getFullYear();
  17. const month = String(date.getMonth() + 1).padStart(2, "0");
  18. const day = String(date.getDate()).padStart(2, "0");
  19. const hours = String(date.getHours()).padStart(2, "0");
  20. const minutes = String(date.getMinutes()).padStart(2, "0");
  21. const seconds = String(date.getSeconds()).padStart(2, "0");
  22. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  23. }
  24. function timestampToString_day(timestamp) {
  25. const date = new Date(timestamp * 1000);
  26. const year = date.getFullYear();
  27. const month = String(date.getMonth() + 1).padStart(2, "0");
  28. const day = String(date.getDate()).padStart(2, "0");
  29. const hours = String(date.getHours()).padStart(2, "0");
  30. const minutes = String(date.getMinutes()).padStart(2, "0");
  31. const seconds = String(date.getSeconds()).padStart(2, "0");
  32. return `${year}-${month}-${day}`;
  33. }
  34. export default { debounce, timestampToString,timestampToString_day };