test_datetime_format.html 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>DateTime Format Test</title>
  5. </head>
  6. <body>
  7. <h1>DateTime Format Test</h1>
  8. <div id="results"></div>
  9. <script>
  10. // 格式化日期时间函数
  11. const formatDateTime = (dateTime) => {
  12. if (!dateTime) return '-'
  13. const date = new Date(dateTime)
  14. // 格式化为 YYYY-MM-DD HH:mm:ss
  15. const year = date.getFullYear()
  16. const month = String(date.getMonth() + 1).padStart(2, '0')
  17. const day = String(date.getDate()).padStart(2, '0')
  18. const hours = String(date.getHours()).padStart(2, '0')
  19. const minutes = String(date.getMinutes()).padStart(2, '0')
  20. const seconds = String(date.getSeconds()).padStart(2, '0')
  21. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  22. }
  23. // 测试数据
  24. const testDates = [
  25. '2026-01-06T17:46:31.000Z',
  26. '2026-01-26T10:30:15.123Z',
  27. '2025-12-25T23:59:59.999Z',
  28. null,
  29. undefined,
  30. ''
  31. ]
  32. const resultsDiv = document.getElementById('results')
  33. testDates.forEach((date, index) => {
  34. const formatted = formatDateTime(date)
  35. const p = document.createElement('p')
  36. p.innerHTML = `<strong>Test ${index + 1}:</strong> Input: ${date} → Output: ${formatted}`
  37. resultsDiv.appendChild(p)
  38. })
  39. // 期望的输出格式示例
  40. const expectedP = document.createElement('p')
  41. expectedP.innerHTML = '<strong>Expected format:</strong> 2026-01-06 17:46:31'
  42. expectedP.style.color = 'green'
  43. expectedP.style.fontWeight = 'bold'
  44. resultsDiv.appendChild(expectedP)
  45. </script>
  46. </body>
  47. </html>