Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell list the dates between a range of dates.

I am writing a script in PowerShell and I need to know the dates between a range of dates.

The script is to automate reporting. Let's say there is a bunch of logged data, but the data only gets collected on weekdays, no weekends and no holidays. I FTP and collect the last 15 generations logged.

With the script I open up the latest instance of the report in Excel. Once the workbook has opened up I read a cell which contains a date, that date will determine when the last time the report has been ran on. (Today's data however will not be accessible until tomorrow, so it is always 1 day behind.)

When I than have that date, I use Get-Date to get the current date. I need to know the dates between the current date and the date I read in from Excel. Afterwards I will need to see if any of the dates have fallen on a holiday, or if they have fell on the weekend.

$excelDate = $excel.Cells.Item(1489, 1).Value()
$currentDate = Get-Date 

Let's say $excelDate = "03/07/2014"

and $currentDate = "03/14/2014"

What I would like to get is 03/08/2014, 03/09/2014, 03/10/2014, 03/11/2014, 03/12/2014, 03/13/2014.

Once I have those dates I can check to see if any of them were a holiday, and if they have fallen on a weekend.

If they fell on a holiday I can subtract 1 day (or 2 days if the last day in the report fell on a Friday...I am still trying to figure out how I am going to do this.), and use a switch to get-content of the proper data before I begin to parse through and put it into Excel.

So something like this:

switch($daysMissed)    
        {                                                       
           1  {Write-Host "The Report is currently up to date!!"                       }   
           2  {$data = get-content C:\Users\$userName\Desktop\Report\data\data.txt  }  
           3  {$data = get-content C:\Users\$userName\Desktop\Report\data\data2.txt } 
           4  {$data = get-content C:\Users\$userName\Desktop\Report\data\data3.txt }
           5  {$data = get-content C:\Users\$userName\Desktop\Report\data\data4.txt }
           6  {$data = get-content C:\Users\$userName\Desktop\Report\data\data5.txt }
           7  {$data = get-content C:\Users\$userName\Desktop\Report\data\data6.txt } 
           8  {$data = get-content C:\Users\$userName\Desktop\Report\data\data7.txt } 
           9  {$data = get-content C:\Users\$userName\Desktop\Report\data\data8.txt } 
           10 {$data = get-content C:\Users\$userName\Desktop\Report\data\data9.txt }
           11 {$data = get-content C:\Users\$userName\Desktop\Report\data\data10.txt }
}

Once I have the proper data, I do a bunch of stuff within Excel and finish off that days report, then I would go $daysMissed = $daysMissed - 1.

So my question is how exactly would I list the dates, between a range of dates?

or, is there an easier way to do what I am doing?

Thanks for the help!

like image 928
BRBT Avatar asked Mar 14 '14 13:03

BRBT


People also ask

How do I compare dates in PowerShell?

Comparing Dates PowerShell knows when a date is “less than” (earlier than) or “greater than” (later than) another date. To compare dates, simply create two DateTime objects using PowerShell Get Date command or perhaps by casting strings with [DateTime] and then using standard PowerShell operators like lt or gt .

How do you use a date range?

By setting date ranges in Excel, we can perform calculations on these dates. For setting date ranges in Excel, we can first format the cells that have a start and end date as 'Date' and then use the operators: '+' or '-'to determine the end date or range duration.

How do you call a date in PowerShell?

Use UFormat Specifier for getting current date and time “%m”: Use this specifier to get the month number: “%d”: “%d” will show the month's day in two digits. “%Y“: This UFormat specifier will show you the current year in four-digit format. “%R“: Use this specifier for displaying time in 24-hour format without seconds.

What is get date in PowerShell?

The Get-Date cmdlet gets a DateTime object that represents the current date or a date that you specify. Get-Date can format the date and time in several . NET and UNIX formats. You can use Get-Date to generate a date or time character string, and then send the string to other cmdlets or programs.


2 Answers

Maybe something like:

$excelDate = get-date "03/07/2014"
$currentDate = get-date "03/14/2014"

for ( $i = $excelDate.AddDays(1); $i -lt $currentDate; $i=$i.AddDays(1) )  { 
    $i.ToShortDateString()
}
like image 59
Hunter Eidson Avatar answered Oct 05 '22 23:10

Hunter Eidson


Something like this, maybe?

$excelDate = "03/07/2014" 
$currentDate = "03/14/2014"

$start = [Datetime]$excelDate
$end   = [Datetime]$currentDate

While ($start -lt $end)
 {
  $start = $start.AddDays(1)
  $start.ToShortDateString()
 }

3/8/2014
3/9/2014
3/10/2014
3/11/2014
3/12/2014
3/13/2014
3/14/2014
like image 26
mjolinor Avatar answered Oct 06 '22 00:10

mjolinor