Lost in DateTime in powershell

huangapple go评论67阅读模式
英文:

Lost in DateTime in powershell

问题

I somehow is lost in DateTime in Powershell

My Function To validate DateTime. Note It was sleeker but I was lost so started explicit returns of Good Bad. Don't Mind that. I want to capture the following

  1. Start Time and End Time entered is correct (not junk)
  2. End Time is bigger than Start Time
  3. End Time is larger than current Time in UTC
  4. Start Time is larger than current time in UTC
function IsValidTimeStamp($startDate,$endDate)
{
    [ref]$parsedDate = (Get-Date).ToUniversalTime()
    if (!([DateTime]::TryParseExact($startDate, "M/d/yy H:mm", [System.Globalization.CultureInfo]::InvariantCulture,
        [System.Globalization.DateTimeStyles]::None,$parseddate))){
            return ("Bad")
        }
    elseif(!([DateTime]::TryParseExact($endDate, "M/d/yy H:mm", [System.Globalization.CultureInfo]::InvariantCulture,
        [System.Globalization.DateTimeStyles]::None,$parseddate))){
            return ("Bad")
        }
    elseif(!(Get-date $([datetime]::ParseExact($endDate, "M/dd/yy H:mm", $Null)) -gt (Get-Date $([datetime]::ParseExact($startDate, "M/dd/yy H:mm", $Null)))){
            return ("Bad")
        }
    elseif((Get-date $([datetime]::ParseExact($startDate, "M/dd/yy H:mm", $Null)).ToUniversalTime() -gt $([System.DateTime]::UtcNow)){
            return ("Bad")
        }
    elseif((Get-date $([datetime]::ParseExact($endDate, "M/dd/yy H:mm", $Null)).ToUniversalTime() -gt $([System.DateTime]::UtcNow)){
            return ("Bad")
        }
    else
        {return ("Good")}
}

Now when I enter the a proper date which is less than current UTC time
$startDate = '03/20/23 14:00' & $endDate = '03/20/23 14:30'
Something is not correct. Most Likely the statement

elseif((Get-date $([datetime]::ParseExact($startDate, "M/dd/yy H:mm", $Null)).ToUniversalTime() -gt $([System.DateTime]::UtcNow)){
            return ("Bad")
        }
elseif((Get-date $([datetime]::ParseExact($endDate, "M/dd/yy H:mm", $Null)).ToUniversalTime() -gt $([System.DateTime]::UtcNow)){
            return ("Bad")
        }

Strangely if you see both the results are returning false as even though I am using a not operator

Lost in DateTime in powershell

I am calling the function like this below

if ((IsValidTimeStamp $($LINE.Start_Time) $($LINE.End_Time)) -eq "Good"){
                #Basic validation of DateTime is successful 
               ###Do something else ###
}
else{
       echo "Failed to validate dateTime"
}

As asked below copy pasting the commands ran on the console

PS D:\Maintenance-Window> $startDate = '03/20/23 14:00';

PS D:\Maintenance-Window> $endDate = '03/20/23 14:30';

PS D:\Maintenance-Window> [ref]$parsedDate = Get-Date;

PS D:\Maintenance-Window> !([DateTime]::TryParseExact($startDate, "M/d/yy H:mm", [System.Globalization.CultureInfo]::InvariantCulture,
        [System.Globalization.DateTimeStyles]::None,$parseddate))
False;

PS D:\Maintenance-Window> !([DateTime]::TryParseExact($endDate, "M/d/yy H:mm", [System.Globalization.CultureInfo]::InvariantCulture,
        [System.Globalization.DateTimeStyles]::None,$parseddate))
False;

PS D:\Maintenance-Window> !(Get-date $([datetime]::ParseExact($endDate, "M/dd/yy H:mm", $Null)) -gt 
            (Get-Date $([datetime]::ParseExact($startDate, "M/dd/yy H:mm", $Null)))
False;

PS D:\Maintenance-Window> (Get-date $([datetime]::ParseExact($startDate, "M/dd/yy H:mm", $Null)).ToUniversalTime() -gt 
            $([System.DateTime]::UtcNow)
False;

PS D:\Maintenance-Window> !(Get-date $([datetime]::ParseExact($startDate, "M/dd/yy H:mm", $Null)).ToUniversalTime() -gt 
            $([System.DateTime]::UtcNow)
False;

If you look closely, the last two commands are exactly the same, one with the Not symbol in front. Since the input time has passed than current UTC time, I expected a True.

EDIT Final Script

function IsValidTimeStamp($startDate,$endDate)
{
    [ref]$parsedDate = (Get-Date).ToUniversalTime()
    if (!([DateTime]::TryParseExact($startDate, "M/d/yy H:mm", [System.Globalization.CultureInfo]::InvariantCulture,
        [System.Globalization.DateTimeStyles]::None,$parseddate))){
            return ("Bad")
        }
    elseif(!([DateTime]::TryParseExact($endDate, "M/d/yy H:mm", [System.Globalization.CultureInfo]::InvariantCulture,
        [System.Globalization.DateTimeStyles]::None,$parseddate))){
            return ("Bad")
        }
    elseif(!((Get-date $([datetime]::ParseExact($endDate, "M/d/yy H:mm", $Null)) -gt (Get-Date $([datetime]::ParseExact($startDate, "M/d/yy H:mm", $Null)))){
            return ("Bad")
        }
    elseif(!((Get-date $([datetime]::ParseExact($startDate, "M/d/yy H:mm", $Null)).ToUniversalTime() -gt $([System.DateTime]::UtcNow))){
            return ("Bad")
        }
    elseif(!((Get-date $([datetime]::ParseExact($endDate, "M/d/yy H:mm", $Null)).ToUniversalTime() -gt $([System.DateTime]::UtcNow))){
            return ("Bad")
        }
    else
        {return ("Good")}
}

(Note: I've corrected the code by removing unnecessary characters like HTML entities.)

英文:

I somehow is lost in DateTime in Powershell

My Function To validate DateTime. Note It was sleeker but I was lost so started explicit returns of Good Bad. Don't Mind that. I want to capture the following

  1. Start Time and End Time entered is correct (not junk)
  2. End Time is bigger than Start Time
  3. End Time is larger than current Time in UTC
  4. Start Time is larger than current time in UTC
function IsValidTimeStamp($startDate,$endDate)
{
    [ref]$parsedDate = (Get-Date).ToUniversalTime()
    if (!([DateTime]::TryParseExact($startDate,"M/d/yy H:mm",[System.Globalization.CultureInfo]::InvariantCulture,
        [System.Globalization.DateTimeStyles]::None,$parseddate))){
            return ("Bad")
        }
    elseif(!([DateTime]::TryParseExact($endDate,"M/d/yy H:mm",[System.Globalization.CultureInfo]::InvariantCulture,
        [System.Globalization.DateTimeStyles]::None,$parseddate))){
            return ("Bad")
        }
    elseif(!(Get-date $([datetime]::ParseExact($endDate,"M/dd/yy H:mm",$Null))) -gt (Get-Date $([datetime]::ParseExact($startDate,"M/dd/yy H:mm",$Null)))){
            return ("Bad")
        }
    elseif((Get-date $([datetime]::ParseExact($startDate,"M/dd/yy H:mm",$Null))).ToUniversalTime() -gt $([System.DateTime]::UtcNow)){
            return ("Bad")
        }
    elseif((Get-date $([datetime]::ParseExact($endDate,"M/dd/yy H:mm",$Null))).ToUniversalTime() -gt $([System.DateTime]::UtcNow)){
            return ("Bad")
        }
    else
        {return ("Good")}
}

Now when I enter the a proper date which is less than current UTC time
$startDate = '03/20/23 14:00' & $endDate = '03/20/23 14:30'
Something is not correct. Most Likely the statement

elseif((Get-date $([datetime]::ParseExact($startDate,"M/dd/yy H:mm",$Null))).ToUniversalTime() -gt $([System.DateTime]::UtcNow)){
            return ("Bad")
        }
 elseif((Get-date $([datetime]::ParseExact($endDate,"M/dd/yy H:mm",$Null))).ToUniversalTime() -gt $([System.DateTime]::UtcNow)){
            return ("Bad")
        }

Strangely if you see both the results are returning false as even though I am using a not operator

Lost in DateTime in powershell

I am calling the function like this below

 if ((IsValidTimeStamp $($LINE.Start_Time) $($LINE.End_Time)) -eq "Good"){
                #Basic validation of DateTime is successful 
               ###Do something else ###
}
else{
       echo "Failed to validate dateTime"
}

As asked below copy pasting the commands ran on the console

PS D:\Maintenance-Window> $startDate = '03/20/23 14:00'

PS D:\Maintenance-Window> $endDate = '03/20/23 14:30'

PS D:\Maintenance-Window> [ref]$parsedDate = Get-Date

PS D:\Maintenance-Window> !([DateTime]::TryParseExact($startDate,"M/d/yy H:mm",[System.Globalization.CultureInfo]::InvariantCulture,
        [System.Globalization.DateTimeStyles]::None,$parseddate))
False

PS D:\Maintenance-Window> !([DateTime]::TryParseExact($endDate,"M/d/yy H:mm",[System.Globalization.CultureInfo]::InvariantCulture,
        [System.Globalization.DateTimeStyles]::None,$parseddate))
False

PS D:\Maintenance-Window> !(Get-date $([datetime]::ParseExact($endDate,"M/dd/yy H:mm",$Null))) -gt `
            (Get-Date $([datetime]::ParseExact($startDate,"M/dd/yy H:mm",$Null)))
False

PS D:\Maintenance-Window> (Get-date $([datetime]::ParseExact($startDate,"M/dd/yy H:mm",$Null))).ToUniversalTime() -gt `
            $([System.DateTime]::UtcNow)
False

PS D:\Maintenance-Window> !(Get-date $([datetime]::ParseExact($startDate,"M/dd/yy H:mm",$Null))).ToUniversalTime() -gt `
            $([System.DateTime]::UtcNow)
False

If you look closely the last two commands are exactly the same one with the Not symbol in front. Since the input time has passed than current UTC time I expected a True.

EDIT Final Script

function IsValidTimeStamp($startDate,$endDate)
{
    [ref]$parsedDate = (Get-Date).ToUniversalTime()
    if (!([DateTime]::TryParseExact($startDate,"M/d/yy H:mm",[System.Globalization.CultureInfo]::InvariantCulture,
        [System.Globalization.DateTimeStyles]::None,$parseddate))){
            return ("Bad")
        }
    elseif(!([DateTime]::TryParseExact($endDate,"M/d/yy H:mm",[System.Globalization.CultureInfo]::InvariantCulture,
        [System.Globalization.DateTimeStyles]::None,$parseddate))){
            return ("Bad")
        }
    elseif(!((Get-date $([datetime]::ParseExact($endDate,"M/d/yy H:mm",$Null))) -gt (Get-Date $([datetime]::ParseExact($startDate,"M/d/yy H:mm",$Null))))){
            return ("Bad")
        }
    elseif(!((Get-date $([datetime]::ParseExact($startDate,"M/d/yy H:mm",$Null))).ToUniversalTime() -gt $([System.DateTime]::UtcNow))){
            return ("Bad")
        }
    elseif(!((Get-date $([datetime]::ParseExact($endDate,"M/d/yy H:mm",$Null))).ToUniversalTime() -gt $([System.DateTime]::UtcNow))){
            return ("Bad")
        }
    else
        {return ("Good")}
}

答案1

得分: 1

function IsValidTimeStamp($startDate, $endDate){ 
try { (get-date) -lt $startDate -and [datetime]$startDate -lt
$enddate } catch { $false } }

get-date
Monday, March 20, 2023 2:13:58 PM

isvalidtimestamp 3/21 3/22
True

isvalidtimestamp 3/21 3/20
False

isvalidtimestamp 3/21 3/21
False

isvalidtimestamp 3/23 3/24
True

isvalidtimestamp 3/23 0/24
False
英文:
function IsValidTimeStamp($startDate, $endDate){ 
try { (get-date) -lt $startDate -and [datetime]$startDate -lt
$enddate } catch { $false } }

get-date
Monday, March 20, 2023 2:13:58 PM

isvalidtimestamp 3/21 3/22
True

isvalidtimestamp 3/21 3/20
False

isvalidtimestamp 3/21 3/21
False

isvalidtimestamp 3/23 3/24
True

isvalidtimestamp 3/23 0/24
False

答案2

得分: 0

When comparing two datetime ranges there are 7 different results.

英文:

When comparing two datetime ranges there are 7 different results

Lost in DateTime in powershell

huangapple
  • 本文由 发表于 2023年3月21日 01:14:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定