Powershell HowTo get last day of 2 month ago - date

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")

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"

$(Get-Date).addDays(-$(Get-Date).Day).addMonths(-1).ToString("yyyyMMdd")

Related

Dynamic variable based on previous month in powershell

I want to change dynamically for below URL.
e.g let's say if I run the script in february, the url would be like below such as m-1-2023.
or if I run the script in march, the url would be like m-2-2023.
if I run the script in january 2023, the url would be like m-12-2022.
and so on.
As summary , it will be the previous month.
My URL:
https://app.contoso.com/api/v1/reports/billing/aws?filter%5Bdate_range%5D=m-1-2023
Thanks,
Use Get-Date to get a DateTime object, which has support for date arithmetics and formatting. Get last month by adding -1 months and format with .Net format strings Like so,
$d = (get-date).AddMonths(-1).ToString("M-yyyy")
$url = "https://app.contoso.com/api/v1/reports/billing/aws?filter%5Bdate_range%5D=m-" + $d
# output
https://app.contoso.com/api/v1/reports/billing/aws?filter%5Bdate_range%5D=m-1-2023

how to get a week's date with autohotkey

i'm using autohotkey to work with date, i need to catch the day a week ago
example
if today is the 28th then I have to take the 21st of last week
calendar
in the following script I take the current date
FormatTime, date, , dd/MM/yyyy
MsgBox %date%
I even thought of a logic, to take the current day subtract by 7 that will take the day a week ago. I need help to create a better script
28 - 7 = 21
if anyone can help me thanks :)
Just subtracting numbers would be bad when you encountered a change between months.
Would have to create custom logic for that.
Luckily AutoHotkey's += operator supports date/time math.
So this is all you need:
;we're starting off the date1 variable as blank,
;which means the current time will be used.
date1 += -7, days
FormatTime, finalDate, % date1, dd/MM/yyyy ;format the result to our desired format
MsgBox, % finalDate
I did it that way
FormatTime, date_, , dd
sub += date_-7
FormatTime date, , /MM/yyyy
MsgBox,%sub%%date%

How to format Get-Date Day as 2 digits in Powershell

I try to get the Day as a two digit number out of Get-Date in PowerShell.
When I try (Get-Date).Day the result will be for example 6 but I want to have it as 06. Also for the month.
How can this be done?
I have already tried things like (Get-Date).Day.ToString("dd") but it doesn't work.
Using ToString() and supplying date formatters (e.g. "yyyy" or "dd") will only work on dates. By accessing .Day or .Year, the operation is instead attempted on an integer, which will fail.
Try (for the day):
(Get-Date).ToString("dd")
...and (and for the month):
(Get-Date).ToString("MM")
See here for custom formatting of dates using ToString()

How to print previous month in PowerShell

I want to display only previous month not with the days and year using PowerShell. I can get the desired result using "(Get-Date).AddMonths(-1)" But this command giving me the complete date.
You can use:
(Get-Date).AddMonths(-1).Month
Which gets you the month number, which, for example, would be 10 for October, if it were currently November.
If you want the month name, you can use:
[CultureInfo]::CurrentCulture.DateTimeFormat.GetMonthName((Get-Date).AddMonths(-1).Month)
Which, for month 10, would give:
October
You were pretty close with what you had! Just need one more parameter:
(Get-Date).AddMonths(-1).Month;
In this case we get a return of 10.
You don't say if you want the previous month number, name or short name, so here they each are:
(Get-Date).AddMonths(-1).Month
(Get-Culture).DateTimeFormat.GetMonthName((Get-Date).AddMonths(-1).Month)
(Get-Culture).DateTimeFormat.GetAbbreviatedMonthName((Get-Date).AddMonths(-1).Month)

How to print current Time and Date in Q Basic?

What is the command for displaying the time and date in qbasic? Could the syntax for the commands be given as well? And an explanation if possible?
You can use DATE$ and TIME$
These can also set the date and time as well.
The command for printing the time(current system time) is time$
The time$ is actually a function, in this case, no parameter is needed.
And the code is...
PRINT TIME$
The time is printed in hh: mm: ss format(hour: minutes: seconds).
And therefore the output would be something like this:
14:55:28
For printing the current system date, we use date$ function which is also a string function
The code is:
PRINT DATE$
The date is printed in mm-dd-yyyy format or month-day-year(American date format).
Hence the output will be:
02-17-2018
Hope it helps...
The QB date/time functions are:
DATE$ returns the date in a string in the form MM-DD-YYYY
TIME$ returns the time in a string in the form HH:MM:SS
When used as a command the date$ and time$ can be assigned to set the system date and time, for example DATE$ = "12-10-1990" or TIME$ = "12:10:10"
If the year is a leap year then the 29th day of February could be set. Otherwise if it is not a leap year then a syntax error will occur trying to set the date in February to the 29th.