web/components/TimerDown/TimerDown.jsx

104 lines
3.5 KiB
JavaScript

"use client";
import Link from "next/link";
import PersianNumber from "plugins/PersianNumber";
import { useEffect, useState } from "react";
const TimerDownBlog = () => {
const [timeRemaining, setTimeRemaining] = useState({
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
milliseconds: 0,
});
useEffect(() => {
const calculateTimeRemaining = () => {
// Get current date and time
const now = new Date();
// Calculate yesterday's date
const yesterday = new Date(now);
yesterday.setDate(now.getDate() - 1);
yesterday.setHours(23, 59, 0, 0); // Set time to 23:59:00
// Calculate tomorrow's date
const tomorrow = new Date(now);
tomorrow.setDate(now.getDate() + 1);
tomorrow.setHours(23, 59, 0, 0); // Set time to 23:59:00
// Calculate time difference
const difference = tomorrow - now;
// Convert time difference to days, hours, minutes, seconds, and milliseconds
const days = Math.floor(difference / (1000 * 60 * 60 * 24));
const hours = Math.floor(
(difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((difference % (1000 * 60)) / 1000);
const milliseconds = difference % 1000;
// Update state
setTimeRemaining({ days, hours, minutes, seconds, milliseconds });
};
// Call calculateTimeRemaining once to set initial state
calculateTimeRemaining();
// Update time remaining every second
const intervalId = setInterval(calculateTimeRemaining, 1000);
// Clean up interval on component unmount
return () => clearInterval(intervalId);
}, []);
return (
<div className=" justify-center ltr realtive mr-5 px-2 mt-[-30px] flex ">
<div className=" rounded-2xl bg-white w-[40px] h-[40px] mx-1">
<p className="mb-0 = text-center text-primary-300 font-bold pt-[10px] ">
<PersianNumber
number={timeRemaining.days}
style={"!text-[15px] pt-[-5px] text-primary-900 "}
/>
</p>
<p className="text-[10px] pt-2 text-center text-white ">روز</p>
</div>
<div className=" rounded-2xl bg-white w-[40px] h-[40px] mx-1">
<p className="mb-0 text-center text-primary-300 font-bold pt-[10px] ">
<PersianNumber
number={timeRemaining.hours}
style={"!text-[15px] pt-[-5px] text-primary-900 "}
/>
</p>
<p className="text-[10px] pt-2 text-center text-white ">ساعت</p>
</div>
<div className=" rounded-2xl bg-white w-[40px] h-[40px] mx-1">
<p className="mb-0 text-center text-primary-300 font-bold pt-[10px] ">
<PersianNumber
number={timeRemaining.minutes}
style={"!text-[15px] pt-[-5px] text-primary-900 "}
/>
</p>
<p className="text-[10px] pt-2 text-center text-white ">دقیقه</p>
</div>
<div className=" rounded-2xl bg-white w-[40px] h-[40px] mx-1">
<Link href="/login">
{" "}
<p className="mb-0 text-center text-primary-300 font-bold pt-[10px] ">
<PersianNumber
number={timeRemaining.seconds}
style={"!text-[15px] pt-[-5px] text-primary-900 "}
/>
</p>
<p className="text-[10px] pt-2 text-center text-white ">ثانیه</p>
</Link>
</div>
</div>
);
};
export default TimerDownBlog;