Home » PowerShell » PowerShell – Convert Datetime to String

PowerShell – Convert Datetime to String

The PowerShell DateTime object has a built-in method that converts datetime to string. The ToString() method of the DateTime object converts a DateTime to a string representation. This method accepts arguments where you can specify the format of the string.

ToString() method allows you to specify the format for the string representation of a DateTime object., you can specify the datetime format like "ddMMyyyy", “dd-MM-yyyy hh:mm:ss” etc…

In this article, we will discuss how to convert date to a string in PowerShell and format date objects in both default and custom format using the ToString method.

PowerShell Convert Date to String

In PowerShell to convert date to string, use the following script that uses the ToString() method of the DateTime object.

# Get the date
$date = Get-Date 

# Convert the datetime to string
$str = $date.ToString()                                                                    

# Print the string represention for the datetime object
$str                                                                                       

# Use the custom format to specify the datetime format

$str = $date.ToString("ddMMyyyy") 

# Use the custom format to specify datetime format
$str = $date.ToString("dd-MM-yyyy hh:mm:ss")

In the above PowerShell script, the Get-Date cmdlet in PowerShell gets the current date and stores it in the $date variable. Using the ToString() method of the DateTime object, it converts a DateTime to a String in PowerShell.

Custom formats specified in the ToString() methods like "ddMMyyyy" and "dd-MM-yyyy hh:mm:ss" convert date to string in PowerShell.

The output of the above PowerShell script to convert date to string is:

PowerShell Convert Date to String
PowerShell Convert Date to String

Another way to convert a datetime object to a string is by using the -f operator. The -f operator performs the string formatting as specified in {0:dd-MM-yyyy}.

$str = '{0:dd-MM-yyyy}' -f (Get-Date '19-01-2023') 

In the above PowerShell script, the Get-Date cmdlet gets the datetime object for the specified date and passes the datetime object to the -f operator, which uses the string format to convert the date to a string in PowerShell.

The output of the above PowerShell script is:

C:\$str = '{0:dd-MM-yyyy}' -f (Get-Date '19-01-2023') 

PS C:\> $str
19-01-2023

Use ToShortDateString() to Convert Date to String in PowerShell

A DateTime object has ToShortDateString() method that converts a datetime object to a string in PowerShell.

# Get the date
$date = Get-Date 

# Use ToShortDateString() to convert datetime to string
$str = $date.ToShortDateString() 

# Print the string
$str

In the above PowerShell script, the Get-Date cmdlet in PowerShell gets the current date and stores it in the $date variable. Use the ToShortDateString() method of the DateTime object to convert the date into the string format.

The output of the above PowerShell script is:

PS C:\> $str = $date.ToShortDateString()                                                           
PS C:\> $str                                                                                       
03-02-2023

Cool Tip: How to convert string to date in PowerShell!

Conclusion

I hope the above article on how to convert datetime to string using DateTime methods like ToString(), ToShortDateString(), and -f operator is helpful to you.

The ToString() method in PowerShell format date objects in default and custom date formats.

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