util.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 formatDate = date => {
  11. const year = date.getFullYear()
  12. const month = date.getMonth() + 1
  13. const day = date.getDate()
  14. return `${[year, month, day].map(formatNumber).join('-')} `
  15. }
  16. const formatNumber = n => {
  17. n = n.toString()
  18. return n[1] ? n : `0${n}`
  19. }
  20. const simleInfo = msg => {
  21. wx.showModal({
  22. title: '提示',
  23. content: msg,
  24. showCancel: false,
  25. success(res) {
  26. }
  27. })
  28. }
  29. const simleInfoWithBack = msg => {
  30. wx.showModal({
  31. title: '提示',
  32. content: msg,
  33. showCancel: false,
  34. success(res) {
  35. wx.navigateBack({})
  36. }
  37. })
  38. }
  39. function formatDateToYMD(dateString) {
  40. if (!dateString) return '';
  41. try {
  42. const date = new Date(dateString);
  43. const year = date.getFullYear();
  44. const month = String(date.getMonth() + 1).padStart(2, '0');
  45. const day = String(date.getDate()).padStart(2, '0');
  46. return `${year}年${month}月${day}日`;
  47. } catch (e) {
  48. console.error('日期格式化错误:', e);
  49. return dateString; // 返回原字符串或自定义错误提示
  50. }
  51. }
  52. module.exports = {
  53. simleInfo: simleInfo,
  54. simleInfoWithBack: simleInfoWithBack,
  55. formatNumber: formatNumber,
  56. formatTime: formatTime,
  57. formatDate: formatDate,
  58. }