How Can I Determine the Beginning and Ending Date of the Previous Month?

Hey, PowerShell! Given a specific date, how can I determine the beginning and ending date of the previous month? In other words, given 8/11/2005, I need to get back 7/1/2005 and 7/31/2005.

Powershell provides access to .Net framework.  We can use .Net framework  DateTime class to solve the problem.  Let's write a script to solve this.

First, we need to get the date for which we need to determine the beginning and ending date of previous month. We can do this using PowerShell Param statement

 

Param ([System.Datetime] $date = $(get-date))

In the above statement, we are declaring a parameter of type Syste.DateTime.  We are also initializing it to current date when no parameter is specified.  It works as follows.  Let's say that we have saved the above as script file  BeginEndPrevMonth.ps1 then

.\BeginEndPrevMonth.ps1 11/18/2006

will set the $date variable to 11/18/2006. However, if you don't specify the parameter like

.\BeginEndPrevMonth.ps1

then PowerShell parser will execute the $(get-date) which will get you the current date and time and this value gets assigned to $date.

 

Let's move on. Now that we have a date, we need to find the beginning and endind date of the previous month of this date. Let's start the end date. if I have a date like 8/11/2005, then I find out the last date of previous month by subtracting the number of days in the current date. In the example, if I subtract 11 then I will get 7/31/2005. We can accomplish this in the script as follows

 

First, determine the number of days in given date

$numdays = $date.Day

Once we have determined the number days,  we need to subtract this from the current date to get the last date of previous month.  There is no subract method available in  Datetime class. however, there is a AddDays method.  We can use this method to subtract days by passing in a  negative value to add. 

 

$prevmonthlastday = $date.AddDays(- $numdays)

We are calling  AddDays method in DateTime with  - $numdays to subtract the number of days.  We use the similar logic to subtract the number of days from  $prevmonthlastday to determine that last day of previou to previous month and then add 1 day to get the first date.

 

 

$numdays = $prevmonthlastday.Day
$prevmonthfirstday = $prevmonthlastday.AddDays( -$numdays + 1)

 

The full script is  as follows

 

 

Param ([system.Datetime] $date = $(get-date))

Write-Output $("The selected date is {0}" -f $date )

$numdays = $date.Day

$prevmonthlastday = $date.AddDays(-$numdays)

Write-Output $("Last date of previous month of given date is {0}" -f $prevmonthlastday )

$numdays = $prevmonthlastday.Day
$prevmonthfirstday = $prevmonthlastday.AddDays( -$numdays + 1)

Write-Output $("First date of previous month of given date is {0}" -f $prevmonthfirstday)

 

There you go ,  a script to determine beginning and ending date of previous month of a given date.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

Arul Kumaravel

Development Manager

Windows PowerShell