在PowerShell中格式化日期

时间:2015-10-14 10:23:54

标签: powershell datetime

我目前正在尝试将日期从regedit中显示的日期转换为可读的日期时间格式。但我不知道该怎么做,我正在使用以下内容:

.GetValue(' InstallDate&#39)

在.csv文件中,它显示为:20150914

我如何将其转换为可读日期?

3 个答案:

答案 0 :(得分:1)

尝试

[datetime]::Parseexact("20150914","yyyyMMdd", $null  )

答案 1 :(得分:1)

我不确定你为什么拒绝投票给另一个答案,因为他对[Datetime]::ParseExact的钱是对的,你将不得不处理空值但是

    $Regbase = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
    foreach ($entry in $regBase)
    {
    $date = (Get-ItemProperty HKLM:\$entry | select installdate).installdate
        try
        {
            [DateTime]::ParseExact($date, "yyyyMMdd", [CultureInfo]::InvariantCulture) 
        }
        catch [exception] 
        {
            "Date Value: $date" 
        }
    }

答案 2 :(得分:0)

PowerShell日期只是.NET DateTime。检查DateTime.ParseExact

[DateTime]::ParseExact("20151010", "yyyyMMdd", [CultureInfo]::InvariantCulture)