'powershell - iterate by days, hours and minutes

I have a very simple script that iterates by days hour by hour:

$Today = [DateTime]"2022-02-19"
$intDays = 0
$intMin = 30
For ($i=0; $i -le $intDays; $i++){
  For ($j=23; $j -ge 0; $j--){

    $StartDate = ($Today.AddDays(-$i)).AddHours($j)
    $EndDate = ($Today.AddDays(-$i)).AddHours($j + 1)

     Write-Host $StartDate `t $EndDate
  }
}

I want to add the third statement, which allows me to iterate by minutes from the $intMin variable. So the sample data would looks like:

2/19/2022 11:30:00 PM    2/20/2022 12:00:00 AM
2/19/2022 11:00:00 PM    2/20/2022 11:30:00 AM
2/19/2022 10:30:00 PM    2/19/2022 11:00:00 PM
2/19/2022 10:00:00 PM    2/19/2022 10:30:00 PM
2/19/2022 9:30:00 PM     2/19/2022 10:00:00 PM

I would appreciate any help with that, regards!



Solution 1:[1]

Although not quite sure what you intend to do with this, but is this what you mean?

$Today   = [DateTime]"2022-02-19"
$intDays = 0
$intMin  = 30

for ($d = 0; $d -le $intDays; $d++){
    for ($h = 23; $h -ge 0; $h--){
        $StartDate = $Today.AddDays(-$d).AddHours($h).AddMinutes($intMin)
        $EndDate = $StartDate.AddMinutes($intMin)
        Write-Host "$StartDate `t $EndDate"
        $StartDate = $StartDate.AddMinutes(-$intMin)
        $EndDate = $StartDate.AddMinutes($intMin)
        Write-Host "$StartDate `t $EndDate"
    }
}

Output:

02/19/2022 11:30:00      02/19/2022 12:00:00
02/19/2022 11:00:00      02/19/2022 11:30:00
02/19/2022 10:30:00      02/19/2022 11:00:00
02/19/2022 10:00:00      02/19/2022 10:30:00
02/19/2022 09:30:00      02/19/2022 10:00:00

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1