Reader small image

You're reading from  Mastering PowerShell Scripting - Fourth Edition

Product typeBook
Published inJun 2021
PublisherPackt
ISBN-139781800206540
Edition4th Edition
Right arrow
Author (1)
Chris Dent
Chris Dent
author image
Chris Dent

Chris Dent is an automation specialist with deep expertise in the PowerShell language. Chris is often found answering questions about PowerShell in both the UK and virtual PowerShell user groups. Chris has been developing in PowerShell since 2007 and has released several modules over the years.
Read more about Chris Dent

Right arrow

Manipulating dates and times

DateTime objects may be created in several ways. The Get-Date command is one of these. The DateTime type has several static methods that can be used, and an instance of DateTime has methods that might be used.

Working with dates includes converting a string representing a date into a DateTime object or finding a date that is relative to the current date and time.

Parsing dates

The Get-Date command is the best first stop for converting strings into dates. Get-Date deals with a reasonable number of formats.

If, however, Get-Date is unable to help, the DateTime class has two static methods that can be used:

  • ParseExact
  • TryParseExact

The format strings used by these methods are documented on Microsoft Docs:

https://docs.microsoft.com/dotnet/standard/base-types/custom-date-and-time-format-strings

The ParseExact method accepts one or more format strings, and returns a DateTime object:

$string = '20170102...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Mastering PowerShell Scripting - Fourth Edition
Published in: Jun 2021Publisher: PacktISBN-13: 9781800206540

Author (1)

author image
Chris Dent

Chris Dent is an automation specialist with deep expertise in the PowerShell language. Chris is often found answering questions about PowerShell in both the UK and virtual PowerShell user groups. Chris has been developing in PowerShell since 2007 and has released several modules over the years.
Read more about Chris Dent