| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <!DOCTYPE html>
- <html>
- <head>
- <title>DateTime Format Test</title>
- </head>
- <body>
- <h1>DateTime Format Test</h1>
- <div id="results"></div>
- <script>
- // 格式化日期时间函数
- const formatDateTime = (dateTime) => {
- if (!dateTime) return '-'
- const date = new Date(dateTime)
-
- // 格式化为 YYYY-MM-DD HH:mm:ss
- const year = date.getFullYear()
- const month = String(date.getMonth() + 1).padStart(2, '0')
- const day = String(date.getDate()).padStart(2, '0')
- const hours = String(date.getHours()).padStart(2, '0')
- const minutes = String(date.getMinutes()).padStart(2, '0')
- const seconds = String(date.getSeconds()).padStart(2, '0')
-
- return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
- }
- // 测试数据
- const testDates = [
- '2026-01-06T17:46:31.000Z',
- '2026-01-26T10:30:15.123Z',
- '2025-12-25T23:59:59.999Z',
- null,
- undefined,
- ''
- ]
- const resultsDiv = document.getElementById('results')
-
- testDates.forEach((date, index) => {
- const formatted = formatDateTime(date)
- const p = document.createElement('p')
- p.innerHTML = `<strong>Test ${index + 1}:</strong> Input: ${date} → Output: ${formatted}`
- resultsDiv.appendChild(p)
- })
- // 期望的输出格式示例
- const expectedP = document.createElement('p')
- expectedP.innerHTML = '<strong>Expected format:</strong> 2026-01-06 17:46:31'
- expectedP.style.color = 'green'
- expectedP.style.fontWeight = 'bold'
- resultsDiv.appendChild(expectedP)
- </script>
- </body>
- </html>
|