转换Fri Nov 02 14:37:02 2018为2018-11-02 14:37:02

时间:2018-11-05 14:27:49

标签: powershell date timestamp logfile

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

1 个答案:

答案 0 :(得分:1)

这是将时间 string 转换为日期时间对象并再次返回的方法... [咧嘴]

$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

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

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