Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Extending PowerShell DateTime Objects

Posted on December 28, 2017

I've been experimenting more with my PSTypeExtensionTools module, finding more objects to enhance.  You can check out the project on Github and install the module from the PowerShell Gallery. My current fun has been with the DateTime object – specifically converting a value into another culture.  Apparently those of us in North America don't know how to format a date properly. But now with a few script methods added to the DateTime type, I can correct that oversight.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

How your dates are formatted is stored in a culture definition.

image

You can get information for other cultures by using the .NET Framework.

image

Let's reformat a date:

image

I can use the –f operator and specify a different pattern.

$n = Get-Date
"{0:$([cultureinfo]::GetCultureInfo("en-gb").datetimeformat.fulldatetimepattern)}" -f $n

image

This isn't especially difficult. But it is a lot to type. So let's make this an extension to the DateTime type with my Add-PSTypeExtension command.

$sb = { 
Param ([string]$culture)

"{0:$([cultureinfo]::GetCultureInfo("$culture").DateTimeFormat.fulldatetimepattern)}" -f $this
}

Add-PSTypeExtension -TypeName System.Datetime -MemberType ScriptMethod -MemberName ConvertCulture -Value $sb

Now this is much easier. All I need to do is invoke the script method and provide a culture.

image

I also added a method to convert to a short date pattern.

$sb2 = {
Param ([string]$culture)

"{0:$([cultureinfo]::GetCultureInfo("$culture").DateTimeFormat.ShortDatePattern)}" -f $this

}

Add-PSTypeExtension -TypeName System.Datetime -MemberType ScriptMethod -MemberName ConvertShortDate -Value $sb2

You use it the same way.

image

I'm not sure of an especially practical use for these methods, but they are fun to play with and help demonstrate the value in extending type definitions. I hope you'll give the PSTypeExtenstionTools module a spin and let me know what you think.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “Extending PowerShell DateTime Objects”

  1. Pete Zerger says:
    December 30, 2017 at 1:17 pm

    Jeff, this is interesting material, and something folks who work with dates would appreciate. Wish you would capture your transcript or otherwise make these samples copy-and-paste friendly so we can get these into our own snippet libraries easily.

    1. Jeffery Hicks says:
      January 2, 2018 at 10:28 am

      You should be able to easily copy code samples. When you expand the code snippet there is a copy icon in the title bar. That should be all you need. At some point I’ll add these as samples to the project as well.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d