Skip to content

Instantly share code, notes, and snippets.

@dstreefkerk
Last active February 9, 2021 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dstreefkerk/c6efb053b27fdc10155a to your computer and use it in GitHub Desktop.
Save dstreefkerk/c6efb053b27fdc10155a to your computer and use it in GitHub Desktop.
A simple PowerShell function to return the week number for a given date using the current globalisation culture.
# Removed my original snippet. See comments below from Bart.
# The below example is courtesy of Bart.
function Get-ISO8601Week {
Param(
[datetime]$DT = (Get-Date)
)
<#
First create an integer(0/1) from the boolean,
"Is the integer DayOfWeek value greater than zero?".
Then Multiply it with 4 or 6 (weekrule = 0 or 2) minus the integer DayOfWeek value.
This turns every day (except Sunday) into Thursday.
Then return the ISO8601 WeekNumber.
#>
$Cult = Get-Culture; $DT = Get-Date($DT)
$WeekRule = $Cult.DateTimeFormat.CalendarWeekRule.value__
$FirstDayOfWeek = $Cult.DateTimeFormat.FirstDayOfWeek.value__
$WeekRuleDay = [int]($DT.DayOfWeek.Value__ -ge $FirstDayOfWeek ) * ( (6 - $WeekRule) - $DT.DayOfWeek.Value__ )
$Cult.Calendar.GetWeekOfYear(($DT).AddDays($WeekRuleDay), $WeekRule, $FirstDayOfWeek)
}
@B4Art
Copy link

B4Art commented Feb 9, 2021

This gives wrong answers:

Get-WeekNumber '2012-12-30'
52
Get-WeekNumber '2012-12-31'
53
Get-WeekNumber '2012-12-30'
1

The way to correct this is:

function Get-ISO8601Week {
     Param(
     [datetime]$DT = (Get-Date)
     )
     <#
     First create an integer(0/1) from the boolean,
     "Is the integer DayOfWeek value greater than zero?".
     Then Multiply it with 4 or 6 (weekrule = 0 or 2) minus the integer DayOfWeek value.
     This turns every day (except Sunday) into Thursday.
     Then return the ISO8601 WeekNumber.
     #>
     $Cult = Get-Culture; $DT = Get-Date($DT)
     $WeekRule = $Cult.DateTimeFormat.CalendarWeekRule.value__
     $FirstDayOfWeek = $Cult.DateTimeFormat.FirstDayOfWeek.value__
     $WeekRuleDay = [int]($DT.DayOfWeek.Value__ -ge $FirstDayOfWeek ) * ( (6 - $WeekRule) - $DT.DayOfWeek.Value__ )
     $Cult.Calendar.GetWeekOfYear(($DT).AddDays($WeekRuleDay), $WeekRule, $FirstDayOfWeek)
}

AND for UICulture it works the same:

function Get-UiISO8601Week {
     Param(
     [datetime]$DT = (Get-Date)
     )
     <#
     First create an integer(0/1) from the boolean,
     "Is the integer DayOfWeek value greater than zero?".
     Then Multiply it with 4 or 6 (weekrule = 0 or 2) minus the integer DayOfWeek value.
     This turns every day (except Sunday) into Thursday.
     Then return the ISO8601 WeekNumber.
     #>
     $Cult = Get-UICulture; $DT = Get-Date($DT)
     $WeekRule = $Cult.DateTimeFormat.CalendarWeekRule.value__
     $FirstDayOfWeek = $Cult.DateTimeFormat.FirstDayOfWeek.value__
     $WeekRuleDay = [int]($DT.DayOfWeek.Value__ -ge $FirstDayOfWeek ) * ( (6 - $WeekRule) - $DT.DayOfWeek.Value__ )
     $Cult.Calendar.GetWeekOfYear(($DT).AddDays($WeekRuleDay), $WeekRule, $FirstDayOfWeek)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment