atownson
atownson

Reputation: 368

Powershell date range from calendar object to array

I have a powershell GUI calendar in which you can select a range of dates. I need to be able to covert that info into an array of dates formatted YYYYMMDD.

I know $Date = "{0:yyyyMMdd}" -f (Get-Date) will work but I can't seem to get the items into the array.

I need an array like this:

20130211
20130212
20130213

Here's the code so far. Thanks for your help!

$Calendar = New-Object System.Windows.Forms.MonthCalendar 
$Calendar.Location = New-Object System.Drawing.Size(10,80)
$Calendar.ShowTodayCircle = $False
$Calendar.MaxDate = (Get-Date).AddDays(1)
$Calendar.MaxSelectionCount = $CalendarSelect
$MenuBox.Controls.Add($Calendar) 
$Dates = $Calendar.SelectionRange

So $Dates will need to be converted into the array. Thanks again!

Upvotes: 1

Views: 2626

Answers (2)

Mervs
Mervs

Reputation: 1

Try this:

$formatted = $dates | select @{name='date';expression={get-date $_.date -f yyyyMMdd}}

Upvotes: 0

zdan
zdan

Reputation: 29450

The SelectionRange property of MonthCalendar returns a SelectionRange object with a Start and End property indicating the start and end of the range. You will need to just loop through the range to get the output you want:

for($d = $dates.Start; $d -le $dates.End; $d = $d.AddDays(1)){get-date $d -Format "yyyyMMdd"}

Upvotes: 1

Related Questions