Categories
Development Powershell Tutorials

Powershell – How to get a Date

In these pandemic times, dating can be difficult but in Powershell it’s as easy as ever!

There are numerous reasons for working with dates in Powershell, two examples could be that you might want to get the current date and time or perhaps convert a weird looking time format to another that you prefer.

Get-DateNow with extra dates

You can run the cmdlet Get-Date just like that and it’ll return the date and time with the current system formatting which on my Swedish computer looks like this: den 21 mars 2021 11:03:30

But when scripting, especially scripts that might end up running on different systems, it’s better to specify the format yourself.
This is easily done by using the -Format parameter and passing in the format of your choosing.

Formats

For me, these are some of the more common formats that I’ve used (except hh since we use a 24h clock here in Sweden).

Format
yyyy
MM
dd
ddd
dddd
HH
hh
mm
ss

Explanation
Year
Month
Day
Day first 3 letters
Day full name
Hours (24h clock)
Hours (12h clock)
Minutes
Seconds

Example
2021
03
21
Sun
Sunday
11
11
20
53

Examples

# No extra formatting
Get-Date 
den 21 mars 2021 11:29:29

# Common formatting
Get-Date -Format "yyyy-MM-dd HH:mm:ss"
2021-03-21 11:30:25

# Better suited for file names, eg. when creating log files. Parameter name shortend to f which also works
Get-Date -f "yyyy-MM-dd_HH.mm.ss"
2021-03-21_11.33.14

# Just the date, full name on both month and day
Get-Date -Format "yyyy MMMM dddd"
2021 March Sunday

# Just the time, no seconds
Get-Date -Format "HH:mm"
11:37

What else can we do? Well you might want to display a future date based on the current date, perhaps for deadlines.
This is easily done with some of the methods that come with this object type, DateTime.
We’ve yet to talk in depth about types, which we will do in a future post, but for now know that the object that is returned from Get-Date is of type DateTime.

# Add 7 days to the current date and then format it
(Get-Date).AddDays(7) | Get-Date -Format "yyyy-MM-dd HH:mm"
2021-03-28 11:51

You also have methods for AddMinutes, AddHours, AddMonths and so on that you can use the same way.
The same method is used when subtracting days, minutes etc. but you enter a negative value.

# Subtract 7 days from the current date and then format it
(Get-Date).AddDays(-7) | Get-Date -Format "yyyy-MM-dd HH:mm"
2021-03-14 11:51

You might need to convert one date format to another, if it is already a DateTime object then it’s not a problem, you just pass it to Get-Date again with the formatting of your choice.
But sometimes the date is just a string of text, maybe in such a weird and jumbled format that Get-Date wouldn’t know what to it with.
Thankfully you can tell Get-Date what the format is so that it can convert it for you.

[DateTime]::ParseExact("02-2019-15_13.37.00","MM-yyyy-dd_HH.mm.ss",$null) | Get-Date -Format "yyyy-MM-dd HH:mm:ss"

We pass in three values here:
“02-2019-15_13.37.00” – The weird date that we want to convert
“MM-yyyy-dd_HH.mm.ss” – The format of the weird date
$null – This is just empty because we do not need it, but this is CultureInfo, in case you’d want to change the CultureInfo of the DateTime object.
For example: [Globalization.CultureInfo]::CreateSpecificCulture(‘sv-SE’)

There is much more you can do with this cmdlet but this is what I would say is the basics that you need and from here I’d suggest you play around with Get-Date.


If you wish any more examples or have any issues please feel free to comment and I’ll see if I can help with it!

As always, continue to learn and evolve your skills, see you in the next one!

Do you want to know more? Here is a list of tutorials to check next

Leave a Reply

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