Dates and time records are nearly everywhere. Files, database entries, logs, you cannot avoid them when dealing with automation. So formatting them the the right way with a fast and powerful tool is key to light and effective code.
PowerShell offers the „Format Operator“ -f ever since and i thought its time to create a list of possibilities, and test them on PowerShell 7 for my own and of course for your reference. The below code was tested on PowerShell 7-preview6 and VS-Code on Windows 10 and MacOS with UI Culture de-DE.
The code is based on the .NET standard base types documentation. Link
First, lets generate a DateTime object of January 1st 2001 to have also days and months with one digits as examples:
1 |
$1jan = Get-Date -Date 1.1.2001 |
General formatting syntax
If you never dealt with the format operator or are new to PowerShell, see an example below how it works. Left is a string „The DateTime is“ with an Index {0} to the date on the right hand side, right is the data we want to format, in our case the current DateTime.
1 2 |
"The DateTime is {0}" -f (Get-Date) The DateTime is 25.11.2019 16:20:59 |
Standard Format Strings
.NET offers a wide range of standard format strings which should cover 99% of your needs when dealing with dates and times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
"{0:d}" -f (Get-Date) # ==> 22.11.2019 "{0:D}" -f (Get-Date) # ==> Freitag, 22. November "{0:t}" -f (Get-Date) # ==> 07:23 "{0:T}" -f (Get-Date) # ==> 07:23:01 "{0:f}" -f (Get-Date) # ==> Freitag, 22.November 2019 07:23 "{0:F}" -f (Get-Date) # ==> Freitag, 22. November 2019 07:21:35 "{0:g}" -f (Get-Date) # ==> 22.11.2019 07:22 "{0:G}" -f (Get-Date) # ==> 22.11.2019 07:22:40 "{0:m}" -f (Get-Date) # ==> 22. November "{0:r}" -f (Get-Date) # ==> Fri, 22 Nov 2019 07:24:08 GMT "{0:s}" -f (Get-Date) # ==> 2019-11-22T07:25:31 "{0:u}" -f (Get-Date) # ==> 2019-11-22 07:26:06Z "{0:U}" -f (Get-Date) # ==> Freitag, 22. November 2019 06:26:27 "{0:y}" -f (Get-Date) # ==> November 2019 |
Custom Format Strings
If you have a more complex demand on date/time formatting, the custom format strings should fulfill your needs. Below find a range of options with examples on custom formatting.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
## Custom format strings "{0:%d}" -f (Get-Date) # ==> 22 "{0:%d}" -f $1jan # ==> 1 (the day - one digit if below 10) "{0:dd}" -f (Get-Date) # ==> 22 "{0:dd}" -f $1jan # ==> 01 (leading zero if 1 digits) "{0:ddd}" -f (Get-Date) # ==> Fr "{0:dddd}" -f (Get-Date) # ==> Freitag "{0:%f}" -f (Get-Date) # ==> 12 (Seconds - no leadin zero) "{0:ff}" -f (Get-Date) # ==> 12 (seconds) "{0:fff}" -f (Get-Date) # ==> 279 (Milliseconds) "{0:ffff}" -f (Get-Date) # ==> 1536 (Microseconds) "{0:fffff}" -f (Get-Date) # ==> usw. "{0:ffffff}" -f (Get-Date) # ==> usw. "{0:fffffff}" -f (Get-Date) # ==> usw. "{0:gg}" -f (Get-Date) # ==> n. Chr. (Anno domini - There are no dates possible before 0001) "{0:%h}" -f (Get-Date) # ==> 7 (the hour - single digit - no leading zero) "{0:hh}" -f (Get-Date) # ==> 07 (the hour) "{0:%m}" -f (Get-Date) # ==> 39 (the minute - single digit - no leading zero) "{0:mm}" -f (Get-Date) # ==> 39 (the minute) "{0:%M}" -f (Get-Date) # ==> 11 (the month) "{0:MM}" -f (Get-Date) # ==> 11 (the month - single digit - no leading zero) "{0:MMM}" -f (Get-Date) # ==> Nov "{0:MMMM}" -f (Get-Date) # ==> November "{0:%s}" -f (Get-Date) # ==> 54 (the second as a single digit - no leading zero) "{0:ss}" -f (Get-Date) # ==> 54 (the second) "{0:%t}" -f (Get-Date) # ==> n (first character of the AM/PM string) "{0:tt}" -f (Get-Date) # ==> nachm. (two characters of the AM/PM string) "{0:%y}" -f (Get-Date) # ==> 19 (year - single digit - no leading zero) "{0:yy}" -f (Get-Date) # ==> 19 (year - two digit) "{0:yyyy}" -f (Get-Date) # ==> 2019 "{0:%z}" -f (Get-Date) # ==> +1 (Timezone Offset) "{0:zz}" -f (Get-Date) # ==> +01 (Timezone Offset with leading zero) "{0:zzz}" -f (Get-Date) # ==> +01:00 (Timezone Offset 4 digits) "|{0: h}|" -f $1jan # ==> 12 (hour with leading blank) "|{0:h }|" -f $1jan # ==> 12 (hour with leading blank) |
Using seperators in Custom Format Strings
If you want to combine date and/or time values in your custom formatting, you need to know there is a time seperator ‚:‘ and a date seperator ‚/‘. Now this lets us combine our custom date/time field to our own string.
1 2 |
"{0:dd/MM/yyy/zz}" -f (Get-Date) # ==> 22.11.2019.+01 (using the Date Seperator '/' and custom formatting) "{0:hh:mm:ffff}" -f (Get-Date) # ==> 07:58:7383 (using custom formatting with the Time Seperator ':') |
Using escape characters in DateTime Strings
We know that there is always more then one path in PowerShell to solve a problem, but you should know, that it is possible to use escape characters in DateTime formatting. This allows us to create some YODA-Style time announcement 🙂
1 |
"{0:h \i\t\ \i\s}" -f $1jan # ==> 12 it is (custom format with escape characters for yoda-style time formatting) |
Hope that helped and is usable to you !
Regards/Roman
Latest Comments