diff --git a/src/native/time/calendarWeek.ts b/src/native/time/calendarWeek.ts index 157446f3cf..0825feb9b2 100644 --- a/src/native/time/calendarWeek.ts +++ b/src/native/time/calendarWeek.ts @@ -3,13 +3,7 @@ * Copyright © 2026 BotForge */ -import { ArgType, NativeFunction, Return } from "../../structures" - -function getWeekOfYear(date: Date) { - const start = new Date(date.getFullYear(), 0, 1) - const days = (date.getTime() - start.getTime()) / 86400000 - return Math.ceil((days + start.getDay() + 1) / 7) -} +import { ArgType, NativeFunction, CalendarType } from "../../structures" export default new NativeFunction({ name: "$calendarWeek", @@ -18,6 +12,24 @@ export default new NativeFunction({ unwrap: true, output: ArgType.Number, execute: async function(ctx) { - return this.success(getWeekOfYear(new Date(new Date().toLocaleString("en-US", { timeZone: ctx.timezone, calendar: ctx.calendar })))) + const date = new Date(new Date().toLocaleString("en-US", { timeZone: ctx.timezone, calendar: ctx.calendar })) + + if (ctx.calendar === CalendarType.Iso8601) { + const target = new Date(date.valueOf()) + const dayNr = (date.getDay() + 6) % 7 + target.setDate(target.getDate() - dayNr + 3) + const firstThursday = target.valueOf() + target.setMonth(0, 1) + if (target.getDay() !== 4) { + target.setMonth(0, 1 + ((4 - target.getDay() + 7) % 7)) + } + return this.success(1 + Math.ceil((firstThursday - target.valueOf()) / 604800000)) + } + + const start = new Date(date.getFullYear(), 0, 1) + const days = (date.getTime() - start.getTime()) / 86400000 + const week = Math.ceil((days + start.getDay() + 1) / 7) + + return this.success(week) } -}) \ No newline at end of file +})