0

I'm trying to write a function to calculate either a full ISO date (yyyy-W-d, where W is the ISO week number), or just the ISO week number by itself. My function is based on this.

I have 2 problems with my code. One is that I do not know how extract either of the results I am after as written above. The other problem is that the second parameter doesn't show when I try to call the function.

This is my current code.

Function Get-ISODate {

    Param(
    [datetime]$Date = (Get-Date),
    [bool]$LongFormat
    )

    $DayOfWeek = $Date.DayOfWeek

    If ($DayOfWeek -match "[1-3]") {$Date.AddDays(3)}

    $Year = $Date.Year

    $Week = $(Get-Culture).Calendar.GetWeekOfYear(($Date),[System.Globalization.CalendarWeekRule]::FirstFourDayWeek, [DayOfWeek]::Monday)

    $Day = $Date.Day

    If ($LongFormat)
    {
    
        Get-ISODate = $Year + '-W' + $Week + '-' + $Day
            
    } 
    
    Else
    {

        Get-ISODate = 'W' + $Week

    }       

}

My expected results would be,

Get-ISOWeek returns W16, which is the current ISO week at this time of writing.

Get-ISOWeek -LongFormat $true returns 2021-W16-1

Get-ISOWeek -Date '2000-1-1' returns W52

Get-ISOWeek -Date '2000-1-1' -LongFormat $true returns 1999-W52-6

I'm using this link to verifiy my results with regards to the week calculation.

Andrew
  • 221
  • 1
  • 10
  • Remove `Get-ISODate = ` – Mathias R. Jessen Apr 19 '21 at 02:31
  • Thanks. I originally tried that, but don't see any output when I "run" the code. Also, the second parameter still doesn't pop up, just the $Date parameter. – Andrew Apr 19 '21 at 02:43
  • `[bool]$LongFormat` should be `[switch]$LongFormat` and you ask `If ($LongFormat.IsPresent){ ....` – Santiago Squarzon Apr 19 '21 at 02:47
  • Thanks. Do you mean ```If ($LongFormat.IsPresent)``` instead of ```If ($LongFormat)```? I tried that, and also replacing ```[bool]$LongFormatP``` with ```[switch]$LongFormat```, but unfortunately I still have the same problems. – Andrew Apr 19 '21 at 02:54
  • @Mathias, I see the biggest issue was I wrote the function incorrectly. (using Get-ISOWeek instead of Get-ISODate) You advice worked for the first problem. – Andrew Apr 19 '21 at 03:00

2 Answers2

1

You can read about switch paramenter here.

What Mathias meant is to literally remove Get-ISODate =

The function should look like this:

Function Get-ISODate {
Param(
    [datetime]$Date = (Get-Date),
    [switch]$LongFormat
)

    $DayOfWeek = $Date.DayOfWeek
    
    If ($DayOfWeek -match "[1-3]") {$Date.AddDays(3)}
    
    $Year = $Date.Year
    $Week = $(Get-Culture).Calendar.GetWeekOfYear(($Date),[System.Globalization.CalendarWeekRule]::FirstFourDayWeek, [DayOfWeek]::Monday)
    $Day = $Date.Day

    If ($LongFormat.IsPresent)
    {
        return "$Year-W$Week-$Day"    
    } 
    
    'W' + $Week
}

# Testing the function
PS /~> Get-IsoDate
W16

PS /~> Get-IsoDate -LongFormat
2021-W16-19
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • Thanks Santiago. That works. I planned on using ```Get-ISODate -LongFormat $true```, but with your solution, I don't have to add the ```$true``` part. – Andrew Apr 19 '21 at 04:29
0

Got this working. Please note that the main issue was that I was trying to call the function incorrectly with "Get-ISOWeek". Monday morning.

I also added .value__ to Day for $Day to get the number instead of the day as "Monday" etc.

Function Get-ISODate {

    Param(
    [datetime]$Date = (Get-Date),
    [bool]$LongFormat
    )

    $DayOfWeek = $Date.DayOfWeek

    If ($DayOfWeek -match "[1-3]") {$Date.AddDays(3)}

    $Year = $Date.Year

    $Week = $(Get-Culture).Calendar.GetWeekOfYear(($Date),[System.Globalization.CalendarWeekRule]::FirstFourDayWeek, [DayOfWeek]::Monday)

    $Day = $Date.DayOfWeek.value__

    If ($LongFormat -eq $true)
    {
    
        "$Year-W$Week-$Day"            
    } 
    
    Else
    {

        "W$Week"

    }       

}

Edit: I realised after posting this, the year was sometimes wrong according to the week number and day of the week. So here is my new version. I have checked the dates here.

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"

    }       

}
Andrew
  • 221
  • 1
  • 10