Andrew
Andrew

Reputation: 219

How to get a result from a function

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.

Upvotes: 0

Views: 84

Answers (2)

Andrew
Andrew

Reputation: 219

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"

    }       

}

Upvotes: 0

Santiago Squarzon
Santiago Squarzon

Reputation: 59900

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

Upvotes: 1

Related Questions