当前位置: 动力学知识库 > 问答 > 编程问答 >

How to add all dates in between two dates to array in powershell

问题描述:

I'm trying to figure out how to add all the dates between 08/27/14 to 10/27/14 to an array in the yyyyMMdd format. Can someone please point me in the right direction.

网友答案:

Thanks guys, as always once I posted on the forum an idea pops into my head. This seems to work for what I need.

$startDate = Get-Date 08-27-14
$endDate = Get-Date 10-27-14
while($startDate -le $endDate){
$nextDate = Get-Date $startDate -format yyyyMMdd
write-host $nextDate
$startDate = $startDate.AddDays(1)
}
网友答案:

Something like this should work:

$date = [datetime]'08/27/2014'

 $array = 
   do {
       $date.ToString('MM/dd/yy')
       $date = $date.AddDays(1)
      }

    until ($date -gt [datetime]'10/27/2014')
网友答案:

An alternative:

$ts = New-TimeSpan -Start (Get-Date -Year 2014 -Month 08 -Day 27) -End (Get-Date -Year 2014 -Month 10 -Day 27)
for ($i = 1; $i -le $ts.Days; $i++) {[array]$dates += ((Get-Date -Year 2014 -Month 08 -Day 27).AddDays($i))}
分享给朋友:
您可能感兴趣的文章:
随机阅读: