Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/native/time/calendarWeek.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
}
})
})