Dates in PowerShell

If you frequently find yourself having to do conversions between various dates and times, here is a handy guide on how to do that quickly in PowerShell.

Built-in Support

PowerShell provides the Get-Date commandlet to help you get started with dates.

To get the current date, simply run Get-Date, and you will see a pretty-printed string like Sunday, October 18, 2020 6:44:30 PM.

UTC and Back

Frequently with working with logs, you will find you have to map between local time and UTC.

A very easy way to get the current time in UTC is to run the [DateTimeOffset]::UtcNow

If you find yourself with a string such as 2020-10-13 23:00:19.0309470 that doesn't have an offset, you can create a local date by running Get-Date -Date "2020-10-13 23:00:19.0309470"

If the date is in UTC and you'd like to see what that was in your local time instead, a quick way to do that is to append a 'Z', and run Get-Date -Date "2020-10-13 23:00:19.0309470z"

If you have a local date and would like to see what the UTC time was then, you can run (Get-Date -Date "mylocaltime").ToUniversalTime()

Date Parts

To take specific parts of a datetime value, you can use various properties such as .Day, .Month, .DayOfWeek, etc. Here are the most commonly used ones.

To see a list of all the parts, run Get-Date | Get-Member.

Formatting

DateTime custom format strings are used a bunch when working with serialization systems. Here is the formatting reference.

Here are examples of some common formats.

$d = get-date

# default is pretty printed
$d
Sunday, October 18, 2020 6:59:38 PM

# use MM for months, mm is for minutes
$d.ToString("yyyy-MM-dd")
2020-10-18

# use HH for 0-23 hours
$d.ToString("yyyy-MM-dd HH:mm:ss")
2020-10-18 18:59:38

# use .ff for milliseconds
$d.ToString("yyyy-MM-dd HH:mm:ss.ff")
2020-10-18 18:59:38.10

# use .fffffff for most precise, and zzz for hour and minute offset
$d.ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz")
2020-10-18T18:59:38.1020074-07:00

# use ddd for a short name-of-week
$d.ToString("ddd yyyy-MM-dd")
Sun 2020-10-18

# use tt for AM/PM
$d.ToString("ddd hh:mm tt")
Sun 06:59 PM

Note that nothing of the above is friendly to localization, which is probably fine for system-to-system communications.

Timezones

OK, so we've covered working with local time and UTC, what about other specific timezones?

You can get a list of registered timezones with [TimeZoneInfo]::GetSystemTimeZones(), but that's a pretty long list. Let's say you want US east time but can't quite remember the name, you can run this instead.

[TimeZoneInfo]::GetSystemTimeZones() | where ID -Like "*east*"

This way, you can see that the timezone you were looking for is US Eastern Standard Time.

Once you have the timezone identifier you want, you can either get a TimeZoneInfo instance, or if you just want to do a quick conversion, you can do it

$d = (get-date)
[TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($d, "Eastern Standard Time")

Sunday, October 18, 2020 9:59:38 PM

Here are some handy identifiers and common cities there.

The short version of 'what time is it in xyz' would be somethig like: [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((get-date), "W. Europe Standard Time")

To do this between any two points, you can specify the source and destination timezones. For example, to find what time it is on the west coast when there's a 5pm even on the east code, you would run the following.

[TimeZoneInfo]::ConvertTimeBySystemTimeZoneId("5pm", "Eastern Standard Time", "Pacific Standard Time")

Sunday, October 18, 2020 2:00:00 PM

If you're going to be doing a bunch of these interactively, it may be handy to create a function around it. This is short enough that you could even put it in a single line.

function e2p {
  param ($t)
  [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($t, "Eastern Standard Time", "Pacific Standard Time")
}

Then, just type e2p "3pm" and get the converted value printed out.

In closing

I encourage experimenting with try.dot.net as well, as it's pretty handy, although of course for simple snippets powershell is great.

Happy times!

Tags:  codingpowershell

Home