util.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 formatDateNotSecond = isoString => {
  11. if (isoString == undefined) return '';
  12. const date = new Date(isoString);
  13. const year = date.getFullYear();
  14. const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are zero-based
  15. const day = String(date.getDate()).padStart(2, '0');
  16. const hours = String(date.getHours()).padStart(2, '0');
  17. const minutes = String(date.getMinutes()).padStart(2, '0');
  18. return `${year}-${month}-${day} ${hours}:${minutes}`;
  19. }
  20. const formatNumber = n => {
  21. n = n.toString()
  22. return n[1] ? n : `0${n}`
  23. }
  24. const formatPhone = phone => {
  25. // 检查手机号是否有效
  26. if (!phone || phone.length !== 11) {
  27. return phone;
  28. }
  29. // 获取手机号前3位和后4位
  30. const start = phone.substring(0, 3);
  31. const end = phone.substring(7);
  32. // 将中间4位数字替换为星号
  33. const hidden = '****';
  34. // 返回处理后的手机号
  35. return start + hidden + end;
  36. }
  37. module.exports = {
  38. formatTime,
  39. formatDateNotSecond,
  40. formatPhone
  41. }