Last active
February 9, 2021 08:31
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gives wrong answers:
The way to correct this is:
AND for UICulture it works the same: