The below code calculates either the full ISO date or ISO Week according to user preferences. $Year, $Month and $Day are set as integers. $Week is not set as anything. If I set $Week as an integer, the code won't add a leading zero to the week number, which is not a deal killer, but ISO weeks are generally formatted that way. If I set it as a string, it results in an error. As better practice, I could use a separate variable to add the leading zero and use that variable in either of the two posible results, but what I really want to know is how I should refer to it. I'd call it a variant in VB langauges, but what is it called in PowerShell?
Function Get-ISODate {
Param(
[datetime]$Date = (Get-Date),
[switch]$LongFormat
)
[int]$Year = $Date.Year
[int]$Month = $Date.Month
[int]$Day = $Date.DayOfWeek.value__
If ($Day -eq 0) {$Day = 7}
If ($Day -ge 1 -and $Day -le 3) {$Date = $Date.AddDays(3)}
$Week = (Get-Culture).Calendar.GetWeekOfYear($Date, 'FirstFourDayWeek', 'Monday')
Switch ($Week)
{
52 {If ($Day -ge 5 -and $Day -le 7 -and $Month -ne 12) {$Year = $Year - 1} ; Break}
53 {If ($Day -ge 5 -and $Day -le 7 -and $Month -ne 12) {$Year = $Year - 1} ; Break}
1 {If ($Month -eq 12) {$Year = $Year + 1} ; Break}
}
$Week = $Week.ToString('00')
If ($LongFormat.IsPresent)
{
"$Year-W$Week-$Day"
}
Else
{
"W$Week"
}
}