Skip to content

Commit

Permalink
✨ Add ISO week conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
brianary committed Sep 23, 2023
1 parent 2622812 commit 28af2ca
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ConvertFrom-IsoWeekDate.ps1
Original file line number Diff line number Diff line change
@@ -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(?<Year>\d+)-W(?<Week>\d\d)-(?<DayOfWeek>\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)}
}

0 comments on commit 28af2ca

Please sign in to comment.