Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell HowTo get last day of 2 month ago

I've a script scheduled every 4th and 14th day of month.

when script starts on 4th, I need to get the last day of the previous month (easy, $a.AddDays(-5))

when script starts on 14th, I need to get the last day of 2 month before.

for example:

 14 april.

 I want to get:28 february 2013

how is it possible? also I want to get the date in format yyyymmdd

UDPDATE:

Is it possible that your solution doesn't work well with the change of the year?

if I'm in january, $(get-date).month -1 results 0. so I did this:

 $datenow = Get-date
 if $datenow.day -eq 14
 $datenow.AddDays(-14)

then I calculate

 $LastDayInMonth = [System.DateTime]::DaysInMonth($(Get-date).Year, $(Get-date.Month))
 $datenow.AddDays(-$LastDayInMonth)
 $datestring = $datenow.ToString("yyyyMMdd")
like image 306
rschirin Avatar asked Apr 30 '13 20:04

rschirin


People also ask

How to Get yesterday's Date in PowerShell?

Get yesterday date using Get-Date in PowerShell You can get yesterday's date using the Get-Date AddDays() method in PowerShell. In the above example, Get-Date is wrapped in parentheses and uses AddDays() method. AddDays() method accepts the input parameter as a number, in this case, -1, and gets yesterday's date.

How to Get-Date using 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.

How do dates work in PowerShell?

The number one command to consider when working with dates and times in PowerShell is the Get-Date command. By running Get-Date just by itself, you will output a DateTime object with the current date and time. Using the Get-Member command, we can see that the output is a System.


2 Answers

To get the date in a string:

$(get-date).Date.ToString("yyyyMMdd")

For getting the last day of two months prior:

if($(get-date).Day -eq 14){
    $LastDayInMonth = [System.DateTime]::DaysInMonth($(get-date).Year, $($(get-date).Month - 2))

}

Update

The line

$LastDayInMonth = [System.DateTime]::DaysInMonth($(get-date).Year, $($(get-date).Month - 2))

Uses the static method DaysInMonth in the System.DateTime class to get the days in the month of the month that is passed to it. It's documentation is here. It takes as input two parameters, the month as an integer, and the year as an integer as well.

In our case, we want 2 months before this month, so for the month parameter we pass in

$(get-date).month - 2

And that is surrounded in a parenthesis to make sure powershell does the calculation and then pass the result of the calculation. get-date is a powershell cmdlet that gives the current date and time as a .NET dateTime object, so we have all the properties and methods at our disposal.

The whole

[System.DateTime]::DaysInMonth()

is just the way of calling static methods in powershell.


Update 2

Once you get the last day in the month, you can concatenate the string by:

$LastDayInMonthString = "$($(get-date).AddMonths(-2).ToString("yyyyMM"))$LastDayInMonth"
like image 128
MDMoore313 Avatar answered Oct 13 '22 23:10

MDMoore313


$(Get-Date).addDays(-$(Get-Date).Day).addMonths(-1).ToString("yyyyMMdd")
like image 45
Gordon Avatar answered Oct 13 '22 23:10

Gordon