Skip to content

Commit

Permalink
fix: current week
Browse files Browse the repository at this point in the history
  • Loading branch information
viet-nv committed Jul 12, 2024
1 parent dec2c06 commit 37f27ce
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/pages/Campaign/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,28 @@ import Leaderboard from './components/Leaderboard'
import { StatCard, Tab, Tabs, Wrapper } from './styles'

function getCurrentWeek(): number {
const now: Date = new Date()
const startOfYear: Date = new Date(now.getFullYear(), 0, 1)
const pastDaysOfYear: number = (now.getTime() - startOfYear.getTime()) / 86400000 // 86400000 ms in a day
const currentDate: Date = new Date()
const startOfYear: Date = new Date(currentDate.getFullYear(), 0, 1)

return Math.ceil((pastDaysOfYear + startOfYear.getDay() + 1) / 7)
// Calculate the day of the week for the start of the year
const dayOfWeek: number = startOfYear.getDay()

// Adjust the start of the year to the nearest Monday
let firstMonday: Date
if (dayOfWeek <= 4) {
firstMonday = new Date(startOfYear.setDate(startOfYear.getDate() - dayOfWeek + 1))
} else {
firstMonday = new Date(startOfYear.setDate(startOfYear.getDate() + (8 - dayOfWeek)))
}

// Calculate the difference in days from the first Monday of the year
const diffInMs: number = currentDate.getTime() - firstMonday.getTime()
const diffInDays: number = Math.floor(diffInMs / (24 * 60 * 60 * 1000))

// Calculate the week number
const weekNumber: number = Math.ceil((diffInDays + 1) / 7)

return weekNumber
}

const weeks = [
Expand Down

0 comments on commit 37f27ce

Please sign in to comment.