PowerShell Convert String to Datetime

Often while working on DateTime based operations, it’s common problems that we face to convert string to DateTime in PowerShell and DateTime to string in PowerShell.

In this article, I will explain you about different ways to convert string to datetime in PowerShell.

You can use ParseExact method of datetime object or cast string to DateTime to convert string to DateTime format.

PowerShell Convert String to DateTime
PowerShell Convert String to DateTime

Convert String to DateTime using ParseExact

DateTime class has ParseExact method which converts the string representation of datetime to DateTime using specified date format.

Let’s consider a string variable having a string representation of datetime. Using ParseExact() method it will convert string to datetime format in PowerShell.

# Using Datetime ParseExact method to convert string to datetime

$strDateTime = '06/08/2021'
[Datetime]::ParseExact($strDateTime, 'dd/MM/yyyy', $null)

In the above PowerShell script, $strDateTime the variable contains datetime format.

Using ParseExact method of DateTime, take a string as input, datetime format, and culture specific format information, and convert string to datetime.

The output of above script in PowerShell to convert string to datetime format as below

PS C:\> # Using Datetime ParseExact method to convert string to datetime

$strDateTime = '06/08/2021'
[Datetime]::ParseExact($strDateTime, 'dd/MM/yyyy', $null)


06 August 2021 00:00:00



PS C:\> 

As in the above output, ParseExact converts string datetime format to dd/MM/yyyy format datetime.

Cool Tip: How to create multiline string in PowerShell!

Convert String to Datetime using Cast

You can cast to convert string to datetime format in PowerShell. cast string converts to DateTime format.

Let’s consider an example below to convert string to datetime using cast

# Using cast string to datetime using DateTime

$strDateTime = "2021-07-02"
[DateTime]$strDateTime

The above PowerShell script, $strDateTime stores string representation of datetime format.

Using [DateTime]string, you can cast string to datetime format.

The output of above convert string to datetime format using cast as below

PS C:\> # Using Datetime ParseExact method to convert string to datetime

$strDateTime = '06/08/2021'
[Datetime]::ParseExact($strDateTime, 'dd/MM/yyyy', $null)


06 August 2021 00:00:00



PS C:\> 

You can store converted string to datetime format date in variable and use datetime operation as below

# Using cast string to datetime using DateTime

$strDateTime = "2021-07-02"
$convertDate = [DateTime]$strDateTime

# Print variable datatype
$convertDate.GetType()

# Convert datetime to dd-mm-yyyy datetime format
'{0:dd-MM-yyyy}' -f $convertDate

# Convert datetime to mm-dd-yyyy datetime format
'{0:MM-dd-yyyy}' -f $convertDate

# Convert datetime to yyyy-mmm-dd datetime format
'{0:yyyy-MMM-dd}' -f $convertDate

In the above PowerShell script, first, it converts a string to DateTime format and stores DateTime value in $convertDate variable.

$convertDate variable has DateTime datatype, use GetType() method to get data type of variable.

Cool Tip: How to execute script with noprofile parameter in PowerShell!

Once you have the DateTime variable, you can do any kind of DateTime operation like print datetime in different datetime formats like dd-mm-yyyy or mm-dd-yyyy or any other valid date time format.

The output of the above PowerShell script as below

IsPublic IsSerial Name              BaseType                                                         
-------- -------- ----              --------                                                         
True     True     DateTime         System.ValueType
                                                 
02-07-2021
07-02-2021
2021-Jul-02

Cool Tip: How to create multiline comments in PowerShell!

Conclusion

I hope the above article on convert string to datetime format using PowerShell is helpful to you while performing datetime operations.

Use DateTime ParseExact() method or cast to Datetime format to convert string to datetime.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment