memoryDay.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // 修改html页面标题
  104. document.title = eventText;
  105. // 解析日期
  106. const startDate = new Date(dateStr);
  107. if (isNaN(startDate.getTime())) {
  108. document.getElementById('start-date').textContent = '日期格式错误';
  109. document.getElementById('time-diff').innerHTML = '<span class="error">日期格式应为: YYYY-MM-DD</span>';
  110. return;
  111. }
  112. // 显示基本信息
  113. document.getElementById('event-text').textContent = eventText;
  114. document.getElementById('start-date').textContent = formatDate(startDate);
  115. document.getElementById('title').textContent = eventText;
  116. // 计算时间差
  117. calculateTimeDifference(startDate);
  118. // 每天更新一次时间差
  119. setInterval(function() {
  120. calculateTimeDifference(startDate);
  121. }, 86400000); // 24小时
  122. });
  123. function calculateTimeDifference(startDate) {
  124. const now = new Date();
  125. // 计算年月日差
  126. let years = now.getFullYear() - startDate.getFullYear();
  127. let months = now.getMonth() - startDate.getMonth();
  128. let days = now.getDate() - startDate.getDate();
  129. // 调整负数月份和日期
  130. if (days < 0) {
  131. months--;
  132. // 获取上个月的天数
  133. const lastMonth = new Date(now.getFullYear(), now.getMonth(), 0);
  134. days += lastMonth.getDate();
  135. }
  136. if (months < 0) {
  137. years--;
  138. months += 12;
  139. }
  140. // 更新显示
  141. document.getElementById('years').textContent = years;
  142. document.getElementById('months').textContent = months;
  143. document.getElementById('days').textContent = days;
  144. // 更新总体时间差显示
  145. let timeDiffText = '已经过去了 ';
  146. if (years > 0) {
  147. timeDiffText += years + ' 年 ';
  148. }
  149. if (months > 0) {
  150. timeDiffText += months + ' 个月 ';
  151. }
  152. if (days > 0) {
  153. timeDiffText += days + ' 天';
  154. }
  155. if (years === 0 && months === 0 && days === 0) {
  156. timeDiffText = '就是今天!';
  157. }
  158. document.getElementById('time-diff').textContent = timeDiffText;
  159. }
  160. function formatDate(date) {
  161. const year = date.getFullYear();
  162. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  163. const day = date.getDate().toString().padStart(2, '0');
  164. return `${year}年${month}月${day}日`;
  165. }
  166. </script>
  167. </body>
  168. </html>