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 in the .NET reference:https://learn.microsoft.com/dotnet/standard/base-types/custom-date-and-time-format-stringsThe ParseExact
method accepts one or more format strings, and returns a DateTime
object:
$string = '20170102-2030'
[DateTime]::ParseExact(
$string,
...