Skip to content

Instantly share code, notes, and snippets.

@petervandivier
Last active May 10, 2023 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petervandivier/276b6610d28182b08c58dd965daabf46 to your computer and use it in GitHub Desktop.
Save petervandivier/276b6610d28182b08c58dd965daabf46 to your computer and use it in GitHub Desktop.
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
}
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