Skip Ribbon Commands
Skip to main content

Quick Launch

Todd Klindt's home page > Todd Klindt's Office 365 Admin Blog > Posts > Wrangling Dates and Times with PowerShell
July 08
Wrangling Dates and Times with PowerShell

PowerShell does a pretty good job handling dates and times due to its good foundation with .NET and its focus on being cool to use PowerShell users. For basic DateTime formatting the help for Get-Date shows some great, easy to use examples.

help Get-Date –Examples

image

There’s –Format, –UFormat, and –DisplayHint, and those are all before we get to the flexibility that is .tostring(). There’s a lot of options, sometimes it’s too many options. Sometimes trying to string together exact combination of time and date information I’m looking for is a lot of work. Once again, PowerShell is there for me in the form of .GetDateTimeFormats()

(Get-Date).GetDateTimeFormats()

It lists out a collection of precanned DateTime formats you can select from:

image

You can use one of those formats just by selecting its index number:

image

That’s the good news. The bad news is that you have to specify which number you want, and there are 133 formats in the list. If the one that really tickles your fancy is #87 you have to count the lines until you get to it.

Until now.

I had danced this dance a few times, squinting to find that just perfect DateTime format I was looking for and counting the lines leading up to it. Then I put my PowerShell mojo to good use and came up with this little gem:

(Get-Date).GetDateTimeFormats() | foreach {$I = 0} { Write-Host "$I - $_" ; $I++ }

Edit 7/9/2019

A helpful reader suggested this even shinier gem:

(Get-Date).GetDateTimeFormats() | foreach-object -begin {$I = 0} -process {[pscustomobject]@{Index = $I;Value = $_}; $I++ }

This lists each format, along with its index number.

image

Once you’ve found the format of your dreams you can look to its left and see what its index is.

(Get-Date).GetDateTimeFormats()[27]

image

If you want to use it with a variable instead of Get-Date, it looks like this:

image

Now you have no excuses for getting exactly the DateTime format you want.

tk

ShortURL: https://www.toddklindt.com/PoshFormatDateTime

Comments

Dates with File output

Just to add my favorites... I tend to use get-date when setting up a regular file output (e.g. export-csv -NoTypeInformation -path "$home\documents\filename$(get-date -format yyyyMMdd).csv")

Then I found get-date -format filedate (or filedatetime)

While testing or outputting multiple files, I use filedatetime, otherwise I stick with filedate. I think filedate and filedatetime are PS 5.1 and up.

Anyhow... just an add to some great info :)
 on 7/9/2019 10:17 AM

Re: Dates with File output

Those are good tips. Thanks.

tk
Todd O. KlindtNo presence information on 7/9/2019 1:53 PM

Just want to say Hi.

This is some great things about Managed Services
and Managed Network Services
 on 7/13/2019 10:48 AM

Norton setup

Norton antivirus software has been eliminating malware, viruses and other kinds of online and offline threats from affecting the performance of a computer
It has a wide range of programs and protection. To know more about Norton antiviruses contact Norton setup, they will assist and provides the best solutions.

https://i-norton.com
 on 7/22/2019 2:06 AM

Quickbooks Technical Support

QuickBooks is used widely as accounting software for managing transaction and invoices of your Company. This is very helpful for small-medium sized business. if you are using QuickBooks and facing any error then you can contact QuickBooks Customer Support for help. They will assist you to solve this issue. You can contact at this for further details:

https://247qbsupport.com/
 on 7/23/2019 6:59 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Today's date *

Select a date from the calendar.
Please enter today's date so I know you are a real person

Twitter


Want a message when I reply to your comment? Put your Twitter handle here.

Attachments

 

 SysKit