Skip to content

Commit 20d3b3d

Browse files
committed
update countdown code, add logo
1 parent cbe428b commit 20d3b3d

File tree

2 files changed

+54
-28
lines changed

2 files changed

+54
-28
lines changed
Lines changed: 1 addition & 0 deletions
Loading

usehooks.com/src/components/CountdownBanner.astro

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,19 @@ import Button from "../components/Button.astro";
210210
</style>
211211

212212
<script>
213-
const targetDate = getPSTDate(2023, 8, 6);
214-
let countdownInterval;
213+
const countdownInterval = setInterval(updateCountdown, 1000);
215214

216-
function updateCountdown() {
217-
const now = new Date() as any;
215+
// updateCountdown();
216+
showCountDown();
218217

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+
);
221224

222-
if (delta <= 0) {
225+
if (hasExpired) {
223226
// Countdown has ended, clear the interval and set all values to 0
224227
clearInterval(countdownInterval);
225228
// document.getElementById("days").textContent = "00";
@@ -229,15 +232,6 @@ import Button from "../components/Button.astro";
229232
return;
230233
}
231234

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-
241235
// Uncomment when we move to the other banner
242236
// document.getElementById("days").textContent = String(days).padStart(2, "0");
243237
document.getElementById("hours").textContent = String(hours).padStart(
@@ -261,19 +255,50 @@ import Button from "../components/Button.astro";
261255
});
262256
}
263257

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();
268264

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();
272272

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;
275277

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+
}
279304
</script>

0 commit comments

Comments
 (0)