Compare Get-Date to Date as String

前端 未结 1 466
Happy的楠姐
Happy的楠姐 2021-01-21 18:06

The registry contains a key, with a string value much like:

23-01-2015 14-58-00

Using PowerShell I need to compare this date to ensure it is gr

相关标签:
1条回答
  • 2021-01-21 18:48

    You'll want to use ParseExact:

    [datetime]::ParseExact("23-01-2015 14-58-00","dd-MM-yyyy HH-mm-ss",$null)
    
    Friday, January 23, 2015 2:58:00 PM
    

    This outputs a DateTime object. "dd-MM-yyyy HH-mm-ss" is a specific mapping to your required DateTime format, using DateTime format placeholders for year, month, day, etc. If I didn't assume the correct format placeholders, just click that link above to find the ones you intended.

    Afterwards, you can compare the two DateTime objects as you expected:

    $myDate = [datetime]::ParseExact("23-01-2015 14-58-00","dd-MM-yyyy HH-mm-ss",$null)
    $myDate -gt (Get-Date).AddDays(-1)
    True
    
    0 讨论(0)
提交回复
热议问题