memoryDay.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>纪念日追踪器</title>
  7. <style>
  8. body {
  9. font-family: 'Microsoft YaHei', Arial, sans-serif;
  10. max-width: 800px;
  11. margin: 0 auto;
  12. padding: 20px;
  13. background-color: #f5f5f5;
  14. color: #333;
  15. }
  16. .container {
  17. background-color: white;
  18. border-radius: 10px;
  19. padding: 30px;
  20. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  21. }
  22. h1 {
  23. color: #e74c3c;
  24. text-align: center;
  25. margin-bottom: 30px;
  26. }
  27. .info-box {
  28. border-left: 4px solid #3498db;
  29. padding: 15px;
  30. margin: 20px 0;
  31. background-color: #f8f9fa;
  32. }
  33. .time-diff {
  34. font-size: 24px;
  35. text-align: center;
  36. margin: 30px 0;
  37. color: #2c3e50;
  38. }
  39. .time-details {
  40. display: flex;
  41. justify-content: center;
  42. gap: 10px;
  43. margin: 20px 0;
  44. }
  45. .time-unit {
  46. text-align: center;
  47. background-color: #3498db;
  48. color: white;
  49. padding: 8px 10px;
  50. border-radius: 5px;
  51. min-width: 60px;
  52. }
  53. .time-unit .value {
  54. font-size: 24px;
  55. font-weight: bold;
  56. }
  57. .time-unit .label {
  58. font-size: 14px;
  59. }
  60. .error {
  61. color: #e74c3c;
  62. text-align: center;
  63. font-weight: bold;
  64. }
  65. </style>
  66. </head>
  67. <body>
  68. <div class="container">
  69. <h1 id="title">纪念日追踪器</h1>
  70. <div class="info-box">
  71. <p>纪念日: <strong id="event-text">加载中...</strong></p>
  72. <p>开始日期: <strong id="start-date">加载中...</strong></p>
  73. </div>
  74. <div class="time-diff" id="time-diff">计算中...</div>
  75. <div class="time-details">
  76. <div class="time-unit">
  77. <div class="value" id="years">-</div>
  78. <div class="label">年</div>
  79. </div>
  80. <div class="time-unit">
  81. <div class="value" id="months">-</div>
  82. <div class="label">月</div>
  83. </div>
  84. <div class="time-unit">
  85. <div class="value" id="days">-</div>
  86. <div class="label">日</div>
  87. </div>
  88. </div>
  89. </div>
  90. <script>
  91. document.addEventListener('DOMContentLoaded', function() {
  92. // 获取URL参数
  93. const urlParams = new URLSearchParams(window.location.search);
  94. const dateStr = urlParams.get('date');
  95. const eventText = urlParams.get('text');
  96. if (!dateStr || !eventText) {
  97. document.getElementById('title').textContent = '参数错误';
  98. document.getElementById('event-text').textContent = '未提供纪念日文本';
  99. document.getElementById('start-date').textContent = '未提供日期';
  100. document.getElementById('time-diff').innerHTML = '<span class="error">请提供正确的URL参数: ?date=YYYY-MM-DD&text=纪念日文本</span>';
  101. return;
  102. }
  103. // 解析日期
  104. const startDate = new Date(dateStr);
  105. if (isNaN(startDate.getTime())) {
  106. document.getElementById('start-date').textContent = '日期格式错误';
  107. document.getElementById('time-diff').innerHTML = '<span class="error">日期格式应为: YYYY-MM-DD</span>';
  108. return;
  109. }
  110. // 显示基本信息
  111. document.getElementById('event-text').textContent = eventText;
  112. document.getElementById('start-date').textContent = formatDate(startDate);
  113. document.getElementById('title').textContent = eventText;
  114. // 计算时间差
  115. calculateTimeDifference(startDate);
  116. // 每天更新一次时间差
  117. setInterval(function() {
  118. calculateTimeDifference(startDate);
  119. }, 86400000); // 24小时
  120. });
  121. function calculateTimeDifference(startDate) {
  122. const now = new Date();
  123. // 计算年月日差
  124. let years = now.getFullYear() - startDate.getFullYear();
  125. let months = now.getMonth() - startDate.getMonth();
  126. let days = now.getDate() - startDate.getDate();
  127. // 调整负数月份和日期
  128. if (days < 0) {
  129. months--;
  130. // 获取上个月的天数
  131. const lastMonth = new Date(now.getFullYear(), now.getMonth(), 0);
  132. days += lastMonth.getDate();
  133. }
  134. if (months < 0) {
  135. years--;
  136. months += 12;
  137. }
  138. // 更新显示
  139. document.getElementById('years').textContent = years;
  140. document.getElementById('months').textContent = months;
  141. document.getElementById('days').textContent = days;
  142. // 更新总体时间差显示
  143. let timeDiffText = '已经过去了 ';
  144. if (years > 0) {
  145. timeDiffText += years + ' 年 ';
  146. }
  147. if (months > 0) {
  148. timeDiffText += months + ' 个月 ';
  149. }
  150. if (days > 0) {
  151. timeDiffText += days + ' 天';
  152. }
  153. if (years === 0 && months === 0 && days === 0) {
  154. timeDiffText = '就是今天!';
  155. }
  156. document.getElementById('time-diff').textContent = timeDiffText;
  157. }
  158. function formatDate(date) {
  159. const year = date.getFullYear();
  160. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  161. const day = date.getDate().toString().padStart(2, '0');
  162. return `${year}年${month}月${day}日`;
  163. }
  164. </script>
  165. </body>
  166. </html>