吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 455|回复: 15
收起左侧

[讨论] 使用HTML搞了一个保质期计算器

[复制链接]
lingdang07 发表于 2025-4-3 16:23
本帖最后由 lingdang07 于 2025-4-3 16:24 编辑

昨天逛超市买了提临期的大米,上面显示18个月保质期,这让我有了一个搞一个保质期计算器的想法,
以下是源代码,希望各位大佬多多指正




[HTML] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>保质期计算器</title>
    <!-- 引入 Font Awesome 图标库 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        :root {
            --primary: #2dd4bf;
            --secondary: #64748b;
            --background: #f1f5f9;
            --white: #ffffff;
            --success: #4ade80;
        }
 
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            /* 修改 max-width,让页面在小屏幕上更窄 */
            max-width: 35%;
            margin: 6rem auto; 
            padding: 10px;
            background-color: var(--background);
            color: var(--secondary);
        }
 
        .calculator {
            background-color: var(--white);
            padding: 1.5rem;
            border-radius: 1.5rem;
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
            animation: fadeIn 0.5s ease-out;
        }
 
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
 
        h2 {
            color: var(--primary);
            text-align: center;
            margin-bottom: 2rem;
            font-size: 2.25rem;
            font-weight: 600;
        }
 
        .form-group {
            margin-bottom: 1.5rem;
        }
 
        label {
            display: block;
            margin-bottom: 0.75rem;
            font-weight: 500;
            color: var(--secondary);
        }
 
        .date-picker-container {
            position: relative;
            cursor: pointer;
            border: 2px solid #e2e8f0;
            border-radius: 0.75rem;
            padding: 1rem 1.5rem;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
        }
 
        .date-picker-container:hover {
            border-color: var(--primary);
            background: rgba(45, 212, 191, 0.05);
        }
 
        #production-date {
            position: absolute;
            opacity: 0;
            width: 100%;
            height: 100%;
            left: 0;
            top: 0;
            cursor: pointer;
        }
 
        .date-display {
            flex: 1;
            font-size: 1rem;
            color: var(--secondary);
        }
 
        input, select {
            width: 100%;
            padding: 1rem;
            border: 2px solid #e2e8f0;
            border-radius: 0.75rem;
            box-sizing: border-box;
            font-size: 1rem;
            transition: border-color 0.3s ease;
        }
 
        input:focus, select:focus {
            outline: none;
            border-color: var(--primary);
        }
 
        button {
            background-color: var(--primary);
            color: var(--white);
            padding: 1rem 2rem;
            border: none;
            border-radius: 0.75rem;
            cursor: pointer;
            width: 100%;
            font-size: 1rem;
            font-weight: 500;
            transition: background-color 0.3s ease;
        }
 
        button:hover {
            background-color: #22c55e;
            transform: translateY(-2px);
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
        }
 
        .result {
            margin-top: 2rem;
            padding: 1.5rem;
            background-color: #f0fdf4;
            border-radius: 0.75rem;
            border-left: 4px solid var(--success);
            display: none;
            animation: slideIn 0.3s ease-out;
        }
 
        @keyframes slideIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
    </style>
</head>
<body>
    <div class="calculator">
        <h2>保质期计算器</h2>
        <div class="form-group">
            <label for="production-date"><i class="fas fa-calendar-day"></i> 生产日期</label>
            <div class="date-picker-container">
                <span class="date-display" id="date-display">点击选择日期</span>
                <input type="date"
                       id="production-date"
                       required
                      >
            </div>
        </div>
        <div class="form-group">
            <label for="shelf-life">保质期时长:</label>
            <div style="display: flex; gap: 1rem;">
                <input type="number" id="shelf-life" min="1" value="1" required>
                <select id="time-unit">
                    <option value="days">天</option>
                    <option value="weeks">周</option>
                    <option value="months">月</option>
                    <option value="years">年</option>
                </select>
            </div>
        </div>
        <button>计算过期日期</button>
        <div class="result" id="result">
            <p>过期日期:<span id="expiry-date"></span></p>
            <p>剩余天数:<span id="days-remaining"></span> 天</p>
        </div>
    </div>
    <script>
        function calculateExpiry() {
            const productionDate = new Date(document.getElementById('production-date').value);
            const shelfLife = parseInt(document.getElementById('shelf-life').value);
            const timeUnit = document.getElementById('time-unit').value;
     
            if (!document.getElementById('production-date').value || isNaN(shelfLife)) {
                alert("请填写所有必填字段");
                return;
            }
     
            const expiryDate = new Date(productionDate);
             
            switch(timeUnit) {
                case 'days':
                    expiryDate.setDate(expiryDate.getDate() + shelfLife);
                    break;
                case 'weeks':
                    expiryDate.setDate(expiryDate.getDate() + (shelfLife * 7));
                    break;
                case 'months':
                    expiryDate.setMonth(expiryDate.getMonth() + shelfLife);
                    break;
                case 'years':
                    expiryDate.setFullYear(expiryDate.getFullYear() + shelfLife);
                    break;
            }
     
            document.getElementById('expiry-date').textContent = expiryDate.toLocaleDateString();
             
            const today = new Date();
            const timeDiff = expiryDate - today;
            const daysRemaining = Math.ceil(timeDiff / (1000 * 3600 * 24));
             
            // 修改显示逻辑
            if (daysRemaining < 0) {
                document.getElementById('days-remaining').innerHTML = `<strong>已过期</strong> ${Math.abs(daysRemaining)} `;
            } else {
                document.getElementById('days-remaining').textContent = daysRemaining;
            }
            document.getElementById('result').style.display = 'block';
        }
     
        document.getElementById('production-date').addEventListener('change', function() {
            const displayElement = document.getElementById('date-display');
            displayElement.textContent = this.value
                ? new Date(this.value).toLocaleDateString('zh-CN', {
                    year: 'numeric',
                    month: 'long',
                    day: 'numeric'
                  })
                : "点击选择日期";
        });
    </script>
</body>
</html>






image.png

image.png

免费评分

参与人数 1热心值 +1 收起 理由
shengruqing + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

cn2jp 发表于 2025-4-3 16:58
感谢作者提供程序!
有个消息是国家已经下发规定,以后食品保质期直接写到期日。
xm65R 发表于 2025-4-3 17:04
xiaoshuimian 发表于 2025-4-3 17:10
feiji144 发表于 2025-4-3 17:24
有时候看保质期挺希望有这个工具的,但是真需要的时候可能就懒得看了
jayjr 发表于 2025-4-3 17:39
好东西,进来学习一下。
fanliansuo1 发表于 2025-4-3 18:56
顶顶顶顶顶顶顶
makaay 发表于 2025-4-3 19:18
简约不简单,小巧、实用、便捷。
Taylor20200522 发表于 2025-4-3 19:45
市面上闹钟提醒软件挺多的,还是感谢分享
lengbingling 发表于 2025-4-3 20:39
生产日期选不了是怎么回事?
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-4-6 22:55

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表