简体   繁体   English

PowerShell:奇怪的日期格式字符串到日期时间

[英]PowerShell: Weird date format string to date time

I am having trouble handling a weird date format exported from an event log. 我在处理从事件日志中导出的奇怪日期格式时遇到麻烦。

It looks like the following: Mon May 14 09:32:59 UTC 2018 看起来像这样: UTC 2018年5月14日星期一09:32:59

And I attempted generic Get-date , all the way to: 我尝试了通用Get-date ,一直到:

$stringToDatetime2 = [Datetime]::ParseExact("$($TaskFailures[0].time)", "ddd MMM dd HH:mm:ss ZZZ yyyy", $null)

I have a feeling I am probably doing something wrong that is obvious. 我有一种可能是做错了很明显的感觉。 That is the type of feeling I am getting. 那就是我得到的那种感觉。

The question you're asking is "how do I get [DateTime]::ParseExact to treat 'UTC' as '+0000'?". 您要问的问题是“如何获取[DateTime]::ParseExact以将'UTC'视为'+0000'?”。

I couldn't get that to work, but I did get the following to work: 我无法解决该问题,但确实做到了以下几点:

[DateTimeOffset]::ParseExact(
    "Mon May 14 09:32:59 UTC 2018".Replace("UTC", "+0000"),
    "ddd MMM dd HH:mm:ss zzz yyyy",
    [CultureInfo]::InvariantCulture)

Note the (cheating) .Replace . 注意(作弊) .Replace

Unfortunately, I can't find a reliable way to recognise all of the timezone abbreviations. 不幸的是,我找不到一种可靠的方式来识别所有时区缩写。 If your log file is only ever in UTC (most are, because servers are usually timezone-agnostic, and configured in UTC), you'll be fine. 如果您的日志文件只使用UTC(大多数时间,因为服务器通常与时区无关,并且使用UTC进行配置),则可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM