Useful commands with Get-Date

In the previous post we saw how to convert a string to the DateTime format.

Now we want to have a string from an actual DateTime object:

$myVar = Get-Date

$temp = '{0:d2}' -f $myVar.Day + "." + '{0:d2}' -f $myVar.Month + "." + '{0:d4}' -f $myVar.Year

$temp

# output:
# 05.11.2023



$temp = (Get-Date).ToString("yyyyMMdd hh:mm:ss")

$temp
# output:
# 20240130 09:55:16



$temp = Get-Date -Format "yyyyMMdd_hhmm"

$temp
# output:
# 20240130_0955
PowerShell

.AddDays, .AddHours etc.:

# yesterday:
$temp = (Get-Date).AddDays(-1)

# output:
# Monday, January 29, 2024 10:06:36 AM
PowerShell

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *