添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

One of my team members recently searched for a solution to convert a string to a date in PowerShell. I suggested different methods. In this tutorial, I will show you how to convert a string to a date in PowerShell with examples.

To convert a string to a date in PowerShell, you can use the [datetime] type accelerator. For example, if you have a date string like "08/19/2024" , you can convert it by using [datetime]::ParseExact("08/19/2024", "MM/dd/yyyy", $null) . This method ensures the string is interpreted correctly according to the specified format.

Table of Contents

Toggle

Convert String to Date in PowerShell

Now, let me show you how to convert a string to a date in PowerShell using different methods.

Method 1: Using Get-Date

The Get-Date cmdlet is useful in PowerShell and can be used to convert a string to a DateTime object.

Here is an example.

$dateString = "08/09/2025"
$date = Get-Date $dateString
Write-Output $date

In this example, the string “08/09/2025” is converted to a DateTime object using Get-Date .

I executed the above PowerShell script, and you can see the output in the screenshot below:

$dateString = "2025-06-09"
$format = "yyyy-MM-dd"
$date = [datetime]::ParseExact($dateString, $format, $null)
Write-Output $date

In this example, the string “2025-06-09” is parsed using the format “yyyy-MM-dd”.

Method 4: Using Get-Date with -Format Parameter

The -Format parameter of Get-Date can be used to specify the desired format of the output.

Here is an example of how we can use the Get-Date with -Format parameter to convert string to date in PowerShell.

$dateString = "06/09/2025"
$date = Get-Date $dateString -Format "yyyy-MM-dd"
Write-Output $date

This converts the string “06/09/2025” to a DateTime object and formats it as “yyyy-MM-dd”.

Here is the output in the screenshot below, you can see:

$dateString = "06/09/2025"
$format = "MM/dd/yyyy"
$date = [datetime]::ParseExact($dateString, $format, $null)
Write-Output $date

Example 2: Convert European Date Format (dd/MM/yyyy)

Here is an example of how to convert string to European Date Format in PowerShell.

$dateString = "09/06/2025"
$format = "dd/MM/yyyy"
$date = [datetime]::ParseExact($dateString, $format, $null)
Write-Output $date

Read Convert String to JSON in PowerShell

Convert string to date with timezone using PowerShell

Now, let me show you how to convert string to date with timezone using PowerShell. There are different methods to do this.

Using ParseExact Method

You can use the ParseExact method to convert string to date with timezone using PowerShell. Here is an example:

$timeString = '2025-08-19 03:00:00 -0400'
$format = 'yyyy-MM-dd HH:mm:ss zzz'
$culture = [System.Globalization.CultureInfo]::InvariantCulture
$datetime = [datetime]::ParseExact($timeString, $format, $culture)

Handle Time Zones

To handle time zones explicitly, you can use the TimeZoneInfo class:

$timeString = '2025-08-19 03:00:00'
$timeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById('Eastern Standard Time')
$datetime = [datetime]::Parse($timeString)
$datetimeWithTimeZone = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($datetime, $timeZone.Id)

Read How to Convert Base64 String to Text in PowerShell?

Convert String to Date without Time using PowerShell

Now, let me show you how to convert string to date without time using PowerShell.

To convert a string to a date without the time component in PowerShell, you can use the [datetime]::ParseExact method. This method allows you to specify the exact format of the date string and parse it accordingly. Once parsed, you can access the .Date property to get just the date part.

Example

Let’s say you have a date string in the format yyyy-MM-dd , and you want to convert it to a date object without the time component.

Here is the complete PowerShell script.

# Define the date string
$dateString = '2025-08-19'
# Define the format of the date string
$format = 'yyyy-MM-dd'
# Parse the date string to a DateTime object
try {
    $dateTime = [datetime]::ParseExact($dateString, $format, [System.Globalization.CultureInfo]::InvariantCulture)
} catch {
    Write-Error "Failed to parse date string: $_"
    return
# Get only the date part
$dateOnly = $dateTime.Date
# Output the result
Write-Output $dateOnly

Explanation

# Parse the date and time string to a DateTime object $dateTime = [datetime]::ParseExact($dateString, $format, [System.Globalization.CultureInfo]::InvariantCulture) # Output the result $dateTime

Explanation

# Parse the date and time string to a DateTime object $dateTime = [datetime]::ParseExact($dateString, $format, [System.Globalization.CultureInfo]::InvariantCulture) # Output the result $dateTime

Explanation

  • Define the Format : The $format variable specifies the format of the date and time string using MM/dd/yyyy hh:mm tt . Here:
  • MM is the two-digit month.
  • dd is the two-digit day.
  • yyyy is the four-digit year.
  • hh is the two-digit hour (12-hour clock).
  • mm is the two-digit minute.
  • tt is the AM/PM designator.
  • # Parse the date and time string to a DateTime object $dateTime = [datetime]::ParseExact($dateString, $format, [System.Globalization.CultureInfo]::InvariantCulture) # Convert the DateTime object to UTC $dateTimeUTC = $dateTime.ToUniversalTime() # Output the result $dateTimeUTC

    Explanation

    # Parse the date and time string to a DateTime object $dateTime = [datetime]::ParseExact($dateString, $format, [System.Globalization.CultureInfo]::InvariantCulture) # Output the result $dateTime

    Explanation

  • Define the Format : The $format variable specifies the format of the date and time string using yyyy-MM-dd HH:mm:ss .
  • yyyy is the four-digit year.
  • MM is the two-digit month.
  • dd is the two-digit day.
  • HH is the two-digit hour in 24-hour format.
  • mm is the two-digit minute.
  • ss is the two-digit second.
  • # Parse the date string to a DateTime object $dateTime = [datetime]::ParseExact($dateString, $format, [System.Globalization.CultureInfo]::InvariantCulture) # Add 10 days to the DateTime object $newDateTime = $dateTime.AddDays(10) # Output the result $newDateTime

    Explanation

    I hope this helps you. Do let me know in the comments below if you have any questions.