将 2018 年 11 月 2 日星期五 14:37:02 转换为 2018-11-02 14:37:02

convert Fri Nov 02 14:37:02 2018 to 2018-11-02 14:37:02

我想在 Windows Powershell 中将 Fri Nov 02 14:37:02 2018 转换为 2018-11-02 14:37:02,感谢任何建议。

这里是如何将时间 string 转换为日期时间对象并再次转换回来的方法... [grin]

$TimeString = 'Fri Nov 02 14:37:02 2018'
$TimeObject = Get-Date -Date '2018-11-02 14:37:02'

'String       = {0}' -f $TimeString
# convert the above string to a datetime object
$TS_DateTimeObject = [datetime]::ParseExact('Fri Nov 02 14:37:02 2018', 'ddd MMM dd HH:mm:ss yyyy', $Null)

# my PC locale is set to use yyyy-MM-dd hh:mm:ss as the datetime format
#    most stateside PC locale setting will be MM-dd-yyyy
'Object       = {0}' -f $TimeObject
'TS_Object    = {0}' -f $TS_DateTimeObject

# convert a datetime object to a string
'DT to String = {0}' -f $TimeObject.ToString('yyyy-MM-dd HH:mm:ss')

输出...

String       = Fri Nov 02 14:37:02 2018
Object       = 2018-11-02 2:37:02 PM
TS_Object    = 2018-11-02 2:37:02 PM
DT to String = 2018-11-02 14:37:02

这是日期时间格式代码的 link ...

日期和时间格式 - PowerShell - SS64.com
https://ss64.com/ps/syntax-dateformats.html