[英]Powershell Converting datetime object to string - How to display time in non-military format?
[英]Converting odd format to [datetime] in powershell
我正在寫一些東西來獲取某個程序的安裝日期,但是我似乎無法將返回的數字轉換為奇怪的日期格式(yyyyMMdd)。 我嘗試將其強制轉換為[datetime]
,但這將返回以下錯誤。
這可能是一個簡單的修復,但是我還沒有遇到過。 有人可以幫忙嗎?
提前致謝!
$test = Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq 'exampleProgram'} | select installdate
[datetime]$test.installdate
Cannot convert value "20160628" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."
At line:1 char:1
+ [datetime]$test.installdate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider`
給定您的日期“ 20160628”,請嘗試:
[datetime]::ParseExact($Date,"yyyyMMdd",$null)
你會得到:
Tuesday, June 28, 2016 12:00:00 AM
您需要使用不變的cultureInfo和自定義格式將日期解析為變量:
$DateTimeVariable = [DateTime]::ParseExact("20160628", "yyyyMMdd", System.Globalization.CultureInfo]::InvariantCulture)
要么
$DateTimeVariable = [DateTime]::ParseExact($test.installdate, "yyyyMMdd", System.Globalization.CultureInfo]::InvariantCulture)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.