Last active
May 10, 2023 17:38
-
-
Save petervandivier/276b6610d28182b08c58dd965daabf46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-DateArray { | |
<# | |
.EXAMPLE | |
$days = New-DateArray '2021-01-01' '2021-02-01' | |
$days | % { $_.ToShortDateString() } | |
#> | |
[CmdletBinding()] | |
Param( | |
[Parameter()] | |
[datetime]$Start = (Get-Date), | |
[Parameter(Mandatory)] | |
[datetime]$End | |
) | |
$numDays = ($End - $Start).Days | |
[datetime[]]$days = $Start | |
1 .. $numDays | ForEach-Object { | |
$days += $days[0].AddDays($_) | |
} | |
$days | Sort-Object | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-TimespanBatchBounds { | |
<# | |
.EXAMPLE | |
$batches = @{ | |
Start = [datetime]::Now.ToShortDateString() | |
End = [datetime]::Now.AddDays(1).ToShortDateString() | |
Step = [timespan]"01:00:00" | |
} | |
New-TimespanBatchBounds @batches | |
#> | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory)] | |
[datetime] | |
$Start, | |
[Parameter(Mandatory)] | |
[datetime] | |
$End, | |
[Parameter(Mandatory)] | |
[timespan] | |
$Step | |
) | |
if($Start -Gt $End){ | |
Write-Error "Start must come before end. You supplied: Start: '$Start', End: '$End'" | |
return | |
} | |
if($Step -gt ($End - $Start)){ | |
Write-Error "Step is greater than total timespan. You supplied: Start: '$Start', End: '$End', Step: '$Step'" | |
return | |
} | |
$boundary = $Start.Add(0) | |
[datetime[]]$bounds = $boundary | |
while($End -Gt $boundary){ | |
$boundary = $boundary.Add($Step) | |
$bounds += $boundary | |
} | |
$bounds | Sort-Object | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment