在 PowerShell 中转换日期时间格式
Converting date time format in PowerShell
如何将以下日期转换成dd/mm/yyyy格式?
Tue Aug 4 17:05:41 2015
我尝试了多种方法和选项,但没有成功。
$a = Get-Date -UFormat %c
(Get-Date $a).ToString("ddMMyyyy")
此日期格式是在日志文件中找到的,我的系统日期时间格式是 dd/mm/yyyy。
我正在尝试对它们进行比较。所以,我需要更改日期时间格式。
可能很原始,但它确实有效:)
$Date = 'Tue Aug 4 17:05:41 2015' -split "\s"
$Year = $Date[-1]
$Time = $Date | ? {$_ -match "..:..:.."}
$DayName = $Date[0]
$Day = $Date[3]
$Month = $Date[1]
Get-Date "$Month $Day $Year $Time" -Format "ddMMyyy"
04082015
您可以使用日期时间 ParseExact 方法:
$us = New-Object system.globalization.cultureinfo("en-US")
[Datetime]::ParseExact('Tue Aug 4 17:05:41 2015', 'ddd MMM d HH:mm:ss yyyy', $us)
如 Vesper 所述,您现在可以比较日期时间对象。
@jisaak 的回答 几乎 准确,除了日期部分前面的额外填充 space("Tue Aug 4 17:05:41 2015" ) 将在您尝试解析每月 10 日和 31 日之间的日期时导致错误:
PS C:\> [Datetime]::ParseExact('Tue Aug 4 17:05:41 2015', 'ddd MMM d HH:mm:ss yyyy', $us)
Tuesday, August 04, 2015 5:05:41 PM
PS C:\> [Datetime]::ParseExact('Tue Aug 11 17:05:41 2015', 'ddd MMM d HH:mm:ss yyyy', $us)
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:1 char:1
+ [Datetime]::ParseExact('Tue Aug 11 17:05:41 2015', 'ddd MMM d HH:mm:ss yyyy', $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
最简单的方法是删除输入字符串和格式字符串中的填充 space:
function Parse-CustomDate {
param(
[Parameter(Mandatory=$true)]
[string]$DateString,
[string]$DateFormat = 'ddd MMM d HH:mm:ss yyyy',
[cultureinfo]$Culture = $(New-Object System.Globalization.CultureInfo -ArgumentList "en-US")
)
# replace double space by a single one
$DateString = $DateString -replace '\s+',' '
[Datetime]::ParseExact($DateString, $DateFormat, $Culture)
}
使用:
Get-Date -format d
这将为您提供今天的日期,但格式为 mm/dd/yyyy
。不过要小心;这会给你 string
而不是 integer
.
如何将以下日期转换成dd/mm/yyyy格式?
Tue Aug 4 17:05:41 2015
我尝试了多种方法和选项,但没有成功。
$a = Get-Date -UFormat %c
(Get-Date $a).ToString("ddMMyyyy")
此日期格式是在日志文件中找到的,我的系统日期时间格式是 dd/mm/yyyy。 我正在尝试对它们进行比较。所以,我需要更改日期时间格式。
可能很原始,但它确实有效:)
$Date = 'Tue Aug 4 17:05:41 2015' -split "\s"
$Year = $Date[-1]
$Time = $Date | ? {$_ -match "..:..:.."}
$DayName = $Date[0]
$Day = $Date[3]
$Month = $Date[1]
Get-Date "$Month $Day $Year $Time" -Format "ddMMyyy"
04082015
您可以使用日期时间 ParseExact 方法:
$us = New-Object system.globalization.cultureinfo("en-US")
[Datetime]::ParseExact('Tue Aug 4 17:05:41 2015', 'ddd MMM d HH:mm:ss yyyy', $us)
如 Vesper 所述,您现在可以比较日期时间对象。
@jisaak 的回答 几乎 准确,除了日期部分前面的额外填充 space("Tue Aug 4 17:05:41 2015" ) 将在您尝试解析每月 10 日和 31 日之间的日期时导致错误:
PS C:\> [Datetime]::ParseExact('Tue Aug 4 17:05:41 2015', 'ddd MMM d HH:mm:ss yyyy', $us)
Tuesday, August 04, 2015 5:05:41 PM
PS C:\> [Datetime]::ParseExact('Tue Aug 11 17:05:41 2015', 'ddd MMM d HH:mm:ss yyyy', $us)
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:1 char:1
+ [Datetime]::ParseExact('Tue Aug 11 17:05:41 2015', 'ddd MMM d HH:mm:ss yyyy', $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
最简单的方法是删除输入字符串和格式字符串中的填充 space:
function Parse-CustomDate {
param(
[Parameter(Mandatory=$true)]
[string]$DateString,
[string]$DateFormat = 'ddd MMM d HH:mm:ss yyyy',
[cultureinfo]$Culture = $(New-Object System.Globalization.CultureInfo -ArgumentList "en-US")
)
# replace double space by a single one
$DateString = $DateString -replace '\s+',' '
[Datetime]::ParseExact($DateString, $DateFormat, $Culture)
}
使用:
Get-Date -format d
这将为您提供今天的日期,但格式为 mm/dd/yyyy
。不过要小心;这会给你 string
而不是 integer
.