From 28af2cad11ea03560b75995b52fbf3858eab33e9 Mon Sep 17 00:00:00 2001 From: Brian Lalonde Date: Fri, 22 Sep 2023 22:21:13 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20ISO=20week=20conversion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConvertFrom-IsoWeekDate.ps1 | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 ConvertFrom-IsoWeekDate.ps1 diff --git a/ConvertFrom-IsoWeekDate.ps1 b/ConvertFrom-IsoWeekDate.ps1 new file mode 100644 index 00000000..00edc22a --- /dev/null +++ b/ConvertFrom-IsoWeekDate.ps1 @@ -0,0 +1,21 @@ +<# +.SYNOPSIS +Returns a DateTime object from an ISO week date string. +#> + +#Requires -Version 3 +[CmdletBinding()] Param( +[Parameter(Position=0,ValueFromPipeline=$true)][ValidatePattern('\A\d+-W\d\d-\d\z')][string] $InputObject +) +Process +{ + if($InputObject -notmatch '\A(?\d+)-W(?\d\d)-(?\d)\z') + { + Stop-ThrowError.ps1 "Unable to parse '$InputObject' as an ISO week date." -Argument InputObject + } + $year,$week,$dow = [int]$Matches.Year,[int]$Matches.Week,[int]$Matches.DayOfWeek + $value = New-Object DateTime $year,1,1 + $startdow = [int]$value.DayOfWeek + if($startdow -gt 4) {return $value.AddDays(7*$week+$dow+(7-$startdow))} + else {return $value.AddDays(7*$week+$dow-$startdow)} +}