How to format Get-Date Day as 2 digits in Powershell - 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()

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 the previous hour time with format in powershell?

I need to find the previous hour time with formatting with PS:
I need the below format:
"yyyy-MM-dd-HH:mm:sstt")
I can use the below code to get the date and time in this format:
(Get-Date -Format "yyyy-MM-dd-HH:mm:sstt")
2019-09-17-08:45:27AM
I need to get the previous hour time but in the above format
I know how to get the last hour time :
(Get-Date).AddHours(-1)
How can i get the previous hour time with a combination of the above
Format?
Using -f, the format operator, as shown in Ivan Mirchev's helpful answer is definitely an option, and -f is a great general-purpose formatting option to know about, for any data type.
However, in your particular case there is a simpler solution, because the .ToString() method of [datetime] instances directly accepts a format string:
(Get-Date).AddHours(-1).ToString('yyyy-MM-dd-HH:mm:sstt')
You may try using the format operator:
"{0:yyyy-MM-dd-HH:mm:sstt}" -f (get-date).AddHours(-1)
more details: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-6#format-operator--f
Hope it helps! :)

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.

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

How do I check if a date in format "yyyymmdd" is a weekday or not?

My program accepts date in the format of "yyyymmdd", I don't know how to check if it's a weekday or not. I've seen ppl using $(date +%u) -gt 5 or "$(date +%a)" in Sat|Sun echo "weekend" in other threads, but that ${date} is like Tue Nov 22 14:16:35 EST 2011 I guess. So is there a good way to convert "yyyymmdd" to ${date} format? Or is there a simple way to check if "yyyymmdd" is a weekday or not? Any language is fine. Thanks.
Any language? In Java I'd use SimpleDateFormat or Joda Time's DateTimeFormatter. In C# I'd either use DateTime.ParseExact or Noda Time's LocalDatePattern. In all of these cases, the result is a value which can be asked for things like the day of the week.
In Python I suspect you want datetime.strptime, e.g.
date = datetime.strptime(text, "%Y%m%d")
day = date.weekday()
if day < 5 # Monday(0) to Friday(4)
# Do something here
This is completely untested, however...
Normally, in a typed language (like Java, C# or Python), you'd 1) initialize your "time" to a language-specific "timedate" type, then 2) get the 'day-of-week" from some method of that time.
It looks like you're using Bourne shell. Which is an untyped language, with no native date/time functions.
Here's one possible solution:
http://unix.ittoolbox.com/groups/technical-functional/shellscript-l/day-of-week-bash-script-2018531
Here are several time/timing related things you can do in the (Linux) Bourne shell:
http://tldp.org/LDP/abs/html/timedate.html