0

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"

    }       

}
Andrew
  • 221
  • 1
  • 10
  • You can still use `[int]` variables and calculate with it if needed and have leading zeros if you use the desired formatting only for the output where you need it. If you need leading zeros you can use the `-f` (format) operator. ... if I got you right. ;-) – Olaf Apr 20 '21 at 07:32
  • Thanks Olaf. I've seen -f before, but I'm not sure how to use it. Could I do it directly with ```"$Year-W$Week-$Day"``` and ```"W$Week"```? – Andrew Apr 20 '21 at 07:37
  • If I got it right you can use this for your `-LongFormat` output: `'{0}-W{1:d2}-{2:d2}' -f $Year, $Week, $Day` – Olaf Apr 20 '21 at 08:13
  • Could you show me where to edit the code? I'd appreciate it because I'm still a PowerShell newbie. – Andrew Apr 20 '21 at 08:22
  • Change this `"$Year-W$Week-$Day"` to this `'{0}-W{1:d2}-{2:d2}' -f $Year, $Week, $Day` – Olaf Apr 20 '21 at 08:31
  • This might be helpful for you as well: [Get the ISO 8601 Week of Year of a given date in Powershell](https://stackoverflow.com/questions/46691143/get-the-iso-8601-week-of-year-of-a-given-date-in-powershell) – Olaf Apr 20 '21 at 08:35
  • And ... if I got it right this should give the desired output as well `Get-Date -UFormat %Y-%W-%d` – Olaf Apr 20 '21 at 08:43
  • Thanks again Olaf. I ended up using ```'{0}-W{1:d2}-{2}' -f $Year, $Week, $Day``` for the full ISO date (no need for a leading zero for the day of the week, just the week number apparently), and ```'W{0:d2}' -f $Week``` if I just want the week number. Unfortunately %W returns the regular week number, but the ISO week number is calculated differently. I did refer to the other link when I wrote my own version. I'm a bit cautious of using some of the "accepted" algorithims, because some of them are not 100% reliable. Fingers crossed that mine is okay. ;-) – Andrew Apr 21 '21 at 00:37
  • By the way, I searched for inormation on using the -f operator, but can't seem to find anything. Do you have any links? I'd like to know more about it. – Andrew Apr 21 '21 at 00:39
  • I've had this experience as well. It helps when you use quotation marks for the operator. [PowerShell "-f" operator](https://www.google.com/search?q=powershell+%22-f%22+operator&oq=powershell+%22-f%22+operator) – Olaf Apr 21 '21 at 00:44
  • Brilliant! Thanks so much Olaf! – Andrew Apr 21 '21 at 02:40

1 Answers1

1

I can't find an explicit reference, but there's a couple of Microsoft articles that call them typed vs untyped, which makes sense...

About Assignment Operators

The assignment by subtraction operator

To delete a variable, use the Remove-Variable cmdlet. This method is useful when the variable is explicitly cast to a particular data type, and you want an untyped variable. The following command deletes the $a variable:

Remove-Variable -Name a

About Script Bblocks

Using delay-bind script blocks with parameters

A typed parameter that accepts pipeline input (by Value) or (by PropertyName) enables use of delay-bind script blocks on the parameter. Within the delay-bind script block, you can reference the piped in object using the pipeline variable $_.

The parameter must not be untyped, and the parameter's type cannot be [scriptblock] or [object].

mclayton
  • 8,025
  • 2
  • 21
  • 26