如何在 PowerShell 中将字符串转换为日期?
我的一位团队成员最近搜索了一种在 PowerShell 中将字符串转换为日期的解决方案。我建议了不同的方法。在本教程中,我将通过示例向您展示如何在 PowerShell 中将字符串转换为日期。
要在 PowerShell 中将字符串转换为日期,您可以使用 [datetime] 类型加速器。例如,如果您有一个类似 "08/19/2024" 的日期字符串,则可以使用 [datetime]::ParseExact("08/19/2024", " MM/dd/yyyy", $null)。此方法确保根据指定的格式正确解释字符串。
在 PowerShell 中将字符串转换为日期
现在,让我向您展示如何使用不同的方法在 PowerShell 中将字符串转换为日期。
方法 1:使用 Get-Date
Get-Date cmdlet 在 PowerShell 中非常有用,可用于将字符串转换为 DateTime 对象。
这是一个例子。
$dateString = "08/09/2025"
$date = Get-Date $dateString
Write-Output $date
在此示例中,使用 Get-Date 将字符串“08/09/2025”转换为 DateTime 对象。
我执行了上面的 PowerShell 脚本,您可以在下面的屏幕截图中看到输出:
方法 2:使用 [datetime]::Parse()
[datetime]::Parse() 方法是在 PowerShell 中将字符串转换为 DateTime 对象的另一种方法。该方法在处理不同的日期格式时特别有用。
这是一个例子。
$dateString = "19-Aug-2025"
$date = [datetime]::Parse($dateString)
Write-Output $date这里,字符串“09-Jun-2025”被解析为 DateTime 对象。
您可以在下面的屏幕截图中看到输出:
查看在 PowerShell 中将字符串转换为双精度
方法 3:使用 [datetime]::ParseExact()
现在,让我向您展示另一种在 PowerShell 中使用 [datetime]::ParseExact() 将字符串转换为日期的方法。此方法要求您指定日期字符串的确切格式。
这是一个例子。
$dateString = "2025-06-09"
$format = "yyyy-MM-dd"
$date = [datetime]::ParseExact($dateString, $format, $null)
Write-Output $date在此示例中,使用格式“yyyy-MM-dd”解析字符串“2025-06-09”。
方法 4:使用带有 -Format 参数的 Get-Date
Get-Date 的 -Format 参数可用于指定所需的输出格式。
以下示例说明了如何在 PowerShell 中使用带有 -Format 参数的 Get-Date 将字符串转换为日期。
$dateString = "06/09/2025"
$date = Get-Date $dateString -Format "yyyy-MM-dd"
Write-Output $date这会将字符串“06/09/2025”转换为 DateTime 对象并将其格式设置为“yyyy-MM-dd”。
这是下面屏幕截图中的输出,您可以看到:
查看在 PowerShell 中将字符串转换为哈希表
处理不同的日期格式
在现实场景中,您可能会遇到各种日期格式。以下是如何处理它们的一些示例:
示例 1:转换美国日期格式 (MM/dd/yyyy)
以下是如何在 PowerShell 中将日期转换为美国日期格式的示例。
$dateString = "06/09/2025"
$format = "MM/dd/yyyy"
$date = [datetime]::ParseExact($dateString, $format, $null)
Write-Output $date示例 2:转换欧洲日期格式 (dd/MM/yyyy)
以下是如何在 PowerShell 中将字符串转换为欧洲日期格式的示例。
$dateString = "09/06/2025"
$format = "dd/MM/yyyy"
$date = [datetime]::ParseExact($dateString, $format, $null)
Write-Output $date阅读在 PowerShell 中将字符串转换为 JSON
使用 PowerShell 将字符串转换为带时区的日期
现在,让我向您展示如何使用 PowerShell 将字符串转换为带时区的日期。有不同的方法可以做到这一点。
使用 ParseExact 方法
您可以使用 PowerShell 使用 ParseExact 方法将字符串转换为带时区的日期。这是一个例子:
$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)处理时区
要明确处理时区,您可以使用 TimeZoneInfo 类:
$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)阅读如何在 PowerShell 中将 Base64 字符串转换为文本?
使用 PowerShell 将字符串转换为不带时间的日期
现在,让我向您展示如何使用 PowerShell 将字符串转换为日期而不需要时间。
要在 PowerShell 中将字符串转换为不带时间部分的日期,您可以使用 [datetime]::ParseExact 方法。此方法允许您指定日期字符串的确切格式并相应地解析它。解析后,您可以访问 .Date 属性以仅获取日期部分。
例子
假设您有一个格式为 yyyy-MM-dd 的日期字符串,并且您希望将其转换为不带时间部分的日期对象。
这是完整的 PowerShell 脚本。
# 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说明
- 定义日期字符串:变量
$dateString以字符串格式保存日期。 - 定义格式:
$format变量指定日期字符串的格式。 - ParseExact 方法:
[datetime]::ParseExact方法用于将字符串转换为DateTime对象。它采用日期字符串、格式和区域性信息对象。 - 提取日期:访问
DateTime对象的.Date属性以仅获取日期部分,从而去除时间部分。
此方法可确保转换准确,并且生成的 DateTime 对象仅包含日期,不包含任何时间信息。
执行上述 PowerShell 脚本后,您可以在下面的屏幕截图中看到输出。
阅读在 PowerShell 中将字符串转换为 Int
PowerShell:将字符串转换为日期 yyyymmddhhmmss
要在 PowerShell 中将 yyyyMMddHHmmss 格式的字符串转换为 DateTime 对象,您可以使用 [datetime]::ParseExact 方法。此方法允许您指定日期和时间字符串的确切格式并相应地解析它。
例子
假设您有一个格式为 yyyyMMddHHmmss 的日期和时间字符串,并且您希望将其转换为 DateTime 对象。这是完整的 PowerShell 脚本。
# Define the date and time string
$dateString = '20240819083045'
# Define the format of the date and time string
$format = 'yyyyMMddHHmmss'
# Parse the date and time string to a DateTime object
$dateTime = [datetime]::ParseExact($dateString, $format, [System.Globalization.CultureInfo]::InvariantCulture)
# Output the result
$dateTime说明
- 定义日期和时间字符串:变量
$dateString保存字符串格式的日期和时间。 - 定义格式:
$format变量使用yyyyMMddHHmmss指定日期和时间字符串的格式。 - ParseExact 方法:
[datetime]::ParseExact方法用于将字符串转换为DateTime对象。它采用日期和时间字符串、格式和区域性信息对象。 - 输出结果:生成的
DateTime对象存储在$dateTime变量中,然后输出。
此方法可确保转换准确,并且生成的 DateTime 对象正确表示字符串中指定的日期和时间。
您还可以在下面的屏幕截图中看到我执行上述 PowerShell 脚本后的输出。
查看如何在 PowerShell 中将字符串转换为布尔值?
使用 PowerShell 将字符串转换为日期时间 AM/PM
要在 PowerShell 中将包含 AM/PM 名称的字符串转换为 DateTime 对象,您可以使用 [datetime]::ParseExact 方法。此方法允许您指定日期和时间字符串的确切格式并相应地解析它。
例子
假设您有一个格式为 MM/dd/yyyy hh:mm tt 的日期和时间字符串(其中 tt 表示 AM/PM 指示符),并且您想要将其转换到 DateTime 对象。
# Define the date and time string
$dateString = '08/19/2024 03:45 PM'
# Define the format of the date and time string
$format = 'MM/dd/yyyy hh:mm tt'
# Parse the date and time string to a DateTime object
$dateTime = [datetime]::ParseExact($dateString, $format, [System.Globalization.CultureInfo]::InvariantCulture)
# Output the result
$dateTime说明
- 定义日期和时间字符串:变量
$dateString保存字符串格式的日期和时间,包括 AM/PM 指示符。 MM是两位数的月份。
dd是两位数的日期。yyyy是四位数的年份。hh是两位数的小时(12 小时制)。mm是两位数的分钟。tt是 AM/PM 指示符。- ParseExact 方法:
[datetime]::ParseExact方法用于将字符串转换为DateTime对象。它采用日期和时间字符串、格式和区域性信息对象。 - 输出结果:生成的
DateTime对象存储在$dateTime变量中,然后输出。
此方法可确保转换准确,并且生成的 DateTime 对象正确表示字符串中指定的日期和时间,包括 AM/PM 指示符。
阅读在 PowerShell 中将字符串转换为小写或大写
PowerShell 将字符串转换为日期时间 UTC
要在 PowerShell 中将字符串转换为 UTC 格式的 DateTime 对象,您可以使用 [datetime]::ParseExact 方法以及 ToUniversalTime 方法。这允许您指定日期字符串的格式,解析它,然后将其转换为 UTC。
让我给你举个例子。
例子
假设您有一个格式为 yyyy-MM-dd HH:mm:ss 的日期和时间字符串,并且您希望将其转换为 UTC 格式的 DateTime 对象。
# Define the date and time string
$dateString = '2024-08-19 15:30:00'
# Define the format of the date and time string
$format = 'yyyy-MM-dd HH:mm:ss'
# 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说明
- 定义日期和时间字符串:变量
$dateString保存字符串格式的日期和时间。 - 定义格式:
$format变量使用yyyy-MM-dd HH:mm:ss指定日期和时间字符串的格式。 - ParseExact 方法:
[datetime]::ParseExact方法用于将字符串转换为DateTime对象。它采用日期和时间字符串、格式和区域性信息对象。 - 转换为 UTC:在
DateTime对象上调用.ToUniversalTime()方法将其转换为 UTC。 - 输出结果:所得的 UTC 格式的
DateTime对象存储在$dateTimeUTC变量中,然后输出。
此方法可确保转换准确,并且生成的 DateTime 对象正确表示 UTC 格式的日期和时间。
查看在 PowerShell 中将变量转换为字符串
使用 PowerShell 将字符串转换为日期时间 24 小时格式
要在 PowerShell 中将 24 小时时间格式的字符串转换为 DateTime 对象,您可以使用 [datetime]::ParseExact 方法。此方法允许您指定日期和时间字符串的确切格式并相应地解析它。
让我给你举个例子。
例子
假设您有一个格式为 yyyy-MM-dd HH:mm:ss 的日期和时间字符串,并且您希望将其转换为 DateTime 对象。
# Define the date and time string
$dateString = '2024-08-19 14:30:00'
# Define the format of the date and time string
$format = 'yyyy-MM-dd HH:mm:ss'
# Parse the date and time string to a DateTime object
$dateTime = [datetime]::ParseExact($dateString, $format, [System.Globalization.CultureInfo]::InvariantCulture)
# Output the result
$dateTime说明
- 定义日期和时间字符串:变量
$dateString保存 24 小时制字符串格式的日期和时间。 yyyy是四位数的年份。
MM是两位数的月份。dd是两位数的日期。HH是 24 小时格式的两位数小时。mm是两位数的分钟。ss是两位数的秒。- ParseExact 方法:
[datetime]::ParseExact方法用于将字符串转换为DateTime对象。它采用日期和时间字符串、格式和区域性信息对象。 - 输出结果:生成的
DateTime对象存储在$dateTime变量中,然后输出。
此方法可确保转换准确,并且生成的 DateTime 对象正确表示字符串中指定的 24 小时格式的日期和时间。
在 PowerShell 中将字符串转换为日期并添加天数
将字符串转换为 DateTime 对象,然后在 PowerShell 中向其添加天数;您可以使用 [datetime]::ParseExact 方法解析字符串,并使用 AddDays 方法向生成的 DateTime 对象添加天数。
让我给你举个例子。
例子
假设您有一个格式为 yyyy-MM-dd 的日期字符串,并且您希望将其转换为 DateTime 对象,然后为其添加 10 天。
# Define the date string
$dateString = '2024-08-19'
# Define the format of the date string
$format = 'yyyy-MM-dd'
# 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说明
- 定义日期字符串:变量
$dateString以字符串格式保存日期。 - 定义格式:
$format变量使用yyyy-MM-dd指定日期字符串的格式。 - ParseExact 方法:
[datetime]::ParseExact方法用于将字符串转换为DateTime对象。它采用日期字符串、格式和区域性信息对象。 - 添加天数:在
DateTime对象上调用.AddDays(10)方法以向其添加 10 天。 - 输出结果:生成的
DateTime对象(加上 10 天)存储在$newDateTime变量中,然后输出。
此方法可确保转换准确,并且生成的 DateTime 对象正确表示添加了指定天数的日期。
结论
在本教程中,我解释了如何使用不同的方法在 PowerShell 中将字符串转换为日期。您可以使用各种方法,例如:
- 使用
Get-Date进行简单转换。 - 使用
[datetime]::Parse()获得更大的灵活性。 - 使用
[datetime]::ParseExact()来精确控制日期格式。 - 将
Get-Date与-Format参数结合使用以获取特定的输出格式。
我还解释了以下示例:
- 使用 PowerShell 将字符串转换为带时区的日期
- 使用 PowerShell 将字符串转换为不带时间的日期
- PowerShell:将字符串转换为日期 yyyymmddhhmmss
- 使用 PowerShell 将字符串转换为日期时间 AM/PM
- PowerShell 将字符串转换为日期时间 UTC
- 使用 PowerShell 将字符串转换为日期时间 24 小时格式
- 在 PowerShell 中将字符串转换为日期并添加天数
我希望这对你有帮助。如果您有任何疑问,请在下面的评论中告诉我。




