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.
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.
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious
Microsoft MVP
award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration.
Read more
.