Powershell Tutorial: How to Convert String to Date?

Check out these expert-recommended methods

by Srishti Sisodia
Srishti Sisodia
Srishti Sisodia
Author
Srishti is an avid writer with a keen interest in technology. She has spent the past ten years of her career researching and writing troubleshooting articles for Windows... read more
Reviewed by Alex Serban
Alex Serban
Alex Serban
Windows Server & Networking Expert
After moving away from the corporate work-style, Alex has found rewards in a lifestyle of constant analysis, team coordination and pestering his colleagues. Holding an MCSA Windows Server... read more
Affiliate Disclosure
  • One of the reasons for converting a string to a date data type is to make arithmetic and comparison operations easier to perform.
  • This guide will discuss all available methods to convert string to date. 
powershell convert string to date

XINSTALL BY CLICKING THE DOWNLOAD FILE
To fix various PC problems, we recommend Restoro PC Repair Tool:
This software will repair common computer errors, protect you from file loss, malware, hardware failure and optimize your PC for maximum performance. Fix PC issues and remove viruses now in 3 easy steps:

  1. Download Restoro PC Repair Tool that comes with Patented Technologies (patent available here).
  2. Click Start Scan to find Windows issues that could be causing PC problems.
  3. Click Repair All to fix issues affecting your computer's security and performance
  • Restoro has been downloaded by 0 readers this month.

Dates are an essential part of several applications and scripts. Still, the date information is usually stored in string format, which makes it difficult to perform operations such as date arithmetic or comparison.

Therefore you need to convert strings to dates; this will make it easier to perform everyday tasks, such as retrieving information about the current date and time, calculating between two date values, and formatting them.

This guide will cover all the simple methods and commands to convert string to date in PowerShell. We will also include examples to make it even easier. Let’s start! 

How can I convert String to Date?

1. Use the Parse command

  1. Press the Windows key, type PowerShell, and click Run as administratorWindows PowerShell -powershell convert string to date
  2. Type the following command after replacing  02/03/2023 with the date you want to convert and press Enter: $dateString = "02/03/2023"
    $parsedDate = [DateTime]::Parse($dateString)
    Write-Output $parsedDate
    powershell_Parse

2. Use the ParseExact command

  1. Launch PowerShell with admin rights.
  2. Copy and paste the following command after replacing  02/03/2023 with the date you want and Enter: $dateString = "02/03/2023"
    $format = "MM/dd/yyyy"
    $parsedDate = [DateTime]::ParseExact($dateString, $format, $null)
    Write-Output $parsedDate
    ParseExact -powershell convert string to date

3. Use the GetDate cmdlet

  1. Open PowerShell with administrator privileges. 
  2. Type the following command after replacing 02/03/2023 with the date you want to convert and Enter:$dateString = "02/03/2023"
    $parsedDate = Get-Date $dateString
    Write-Output $parsedDate
    Get DAte -powershell convert string to date

4. Use the DateTime Accelerator

  1. Launch PowerShell with admin rights. 
  2. Copy and paste the following command, but replace 02/03/2023 with the date you want and press Enter: $dateString = "02/03/2023"
    $parsedDate = [DateTime]$dateString
    Write-Output $parsedDate
    DateTime

5. Use the Cast function

  1. Open PowerShell with administrator rights. 
  2. Type the following command after replacing  02/03/2023 with the date you want and press Enter: $dateString = "02/03/2023"
    $parsedDate = [DateTime]$dateString
    Write-Output $parsedDate
    CAST

So, these are the standard methods you can use to convert string to date in PowerShell. If you have any questions or concerns about the process, feel free to mention them in the comments section below.

Still having issues? Fix them with this tool:

SPONSORED

If the advices above haven't solved your issue, your PC may experience deeper Windows problems. We recommend downloading this PC Repair tool (rated Great on TrustPilot.com) to easily address them. After installation, simply click the Start Scan button and then press on Repair All.

This article covers:Topics: