@@ -210,16 +210,19 @@ import Button from "../components/Button.astro";
210
210
</style >
211
211
212
212
<script >
213
- const targetDate = getPSTDate(2023, 8, 6);
214
- let countdownInterval;
213
+ const countdownInterval = setInterval(updateCountdown, 1000);
215
214
216
- function updateCountdown() {
217
- const now = new Date() as any ;
215
+ // updateCountdown();
216
+ showCountDown() ;
218
217
219
- // Calculate the difference in seconds
220
- let delta = Math.floor((targetDate - now) / 1000);
218
+ function updateCountdown() {
219
+ const { days, hours, minutes, seconds, hasExpired } = timeUntilTargetPST(
220
+ 2023,
221
+ 9,
222
+ 12
223
+ );
221
224
222
- if (delta <= 0 ) {
225
+ if (hasExpired ) {
223
226
// Countdown has ended, clear the interval and set all values to 0
224
227
clearInterval(countdownInterval);
225
228
// document.getElementById("days").textContent = "00";
@@ -229,15 +232,6 @@ import Button from "../components/Button.astro";
229
232
return;
230
233
}
231
234
232
- // Calculate days, hours, minutes and seconds
233
- const days = Math.floor(delta / 86400);
234
- delta -= days * 86400;
235
- const hours = Math.floor(delta / 3600) % 24;
236
- delta -= hours * 3600;
237
- const minutes = Math.floor(delta / 60) % 60;
238
- delta -= minutes * 60;
239
- const seconds = delta % 60;
240
-
241
235
// Uncomment when we move to the other banner
242
236
// document.getElementById("days").textContent = String(days).padStart(2, "0");
243
237
document.getElementById("hours").textContent = String(hours).padStart(
@@ -261,19 +255,50 @@ import Button from "../components/Button.astro";
261
255
});
262
256
}
263
257
264
- function getPSTDate(year, month, day, hour = 0, minute = 0, second = 0): any {
265
- const utcDate = new Date(
266
- Date.UTC(year, month, day, hour + 8, minute, second)
267
- );
258
+ function timeUntilTargetPST(
259
+ targetYear: number,
260
+ targetMonth: number,
261
+ targetDay: number
262
+ ) {
263
+ const now = new Date();
268
264
269
- const pstString = utcDate.toLocaleString("en-US", {
270
- timeZone: "America/Los_Angeles",
271
- });
265
+ // Extract the UTC year, month, day, hour, minute, and second
266
+ const year = now.getUTCFullYear();
267
+ const month = now.getUTCMonth();
268
+ const date = now.getUTCDate();
269
+ const hour = now.getUTCHours();
270
+ const minute = now.getUTCMinutes();
271
+ const second = now.getUTCSeconds();
272
272
273
- return new Date(pstString);
274
- }
273
+ // Create a current UTC date object
274
+ const currentUTC = new Date(
275
+ Date.UTC(year, month, date, hour, minute, second)
276
+ ) as any;
275
277
276
- updateCountdown();
277
- showCountDown();
278
- countdownInterval = setInterval(updateCountdown, 1000);
278
+ // Construct the target UTC date at 8 AM UTC (midnight PST)
279
+ const targetUTC = new Date(
280
+ Date.UTC(targetYear, targetMonth - 1, targetDay, 8, 0, 0)
281
+ ) as any;
282
+
283
+ let delta = (targetUTC - currentUTC) / 1000;
284
+
285
+ const days = Math.floor(delta / 86400);
286
+ delta -= days * 86400;
287
+
288
+ const hours = Math.floor(delta / 3600) % 24;
289
+ delta -= hours * 3600;
290
+
291
+ const minutes = Math.floor(delta / 60) % 60;
292
+ delta -= minutes * 60;
293
+
294
+ const seconds = delta % 60;
295
+
296
+ return {
297
+ days: String(days).padStart(2, "0"),
298
+ hours: String(hours).padStart(2, "0"),
299
+ minutes: String(minutes).padStart(2, "0"),
300
+ seconds: String(seconds).padStart(2, "0"),
301
+ hasExpired: delta <= 0,
302
+ };
303
+ }
279
304
</script >
0 commit comments