diff --git a/src/assets/svg/other/timer.svg b/src/assets/svg/other/timer.svg new file mode 100644 index 0000000..2ad97bc --- /dev/null +++ b/src/assets/svg/other/timer.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/components/Sidebar/RunTime/index.tsx b/src/components/Sidebar/RunTime/index.tsx new file mode 100644 index 0000000..bee11a1 --- /dev/null +++ b/src/components/Sidebar/RunTime/index.tsx @@ -0,0 +1,41 @@ +"use client" + +import { useState } from 'react'; +import Image from 'next/image'; +import Timer from '@/assets/svg/other/timer.svg'; + +export default () => { + const [time, setTime] = useState(1547647320000); + + const calculateTimeDifference = (startTimestamp: number) => { + const startDate = new Date(startTimestamp); + const currentDate = new Date(); + + let years = currentDate.getFullYear() - startDate.getFullYear(); + let months = currentDate.getMonth() - startDate.getMonth(); + let days = currentDate.getDate() - startDate.getDate(); + + // 处理月份和天数的进位 + if (days < 0) { + const lastMonth = new Date(currentDate.getFullYear(), currentDate.getMonth(), 0); + days += lastMonth.getDate(); + months--; + } + if (months < 0) { + months += 12; + years--; + } + + return `${years}年 ${months}个月 ${days}天`; + }; + + return ( +