Lost in DateTime in powershell

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

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
  1. function IsValidTimeStamp($startDate,$endDate)
  2. {
  3. [ref]$parsedDate = (Get-Date).ToUniversalTime()
  4. if (!([DateTime]::TryParseExact($startDate, "M/d/yy H:mm", [System.Globalization.CultureInfo]::InvariantCulture,
  5. [System.Globalization.DateTimeStyles]::None,$parseddate))){
  6. return ("Bad")
  7. }
  8. elseif(!([DateTime]::TryParseExact($endDate, "M/d/yy H:mm", [System.Globalization.CultureInfo]::InvariantCulture,
  9. [System.Globalization.DateTimeStyles]::None,$parseddate))){
  10. return ("Bad")
  11. }
  12. elseif(!(Get-date $([datetime]::ParseExact($endDate, "M/dd/yy H:mm", $Null)) -gt (Get-Date $([datetime]::ParseExact($startDate, "M/dd/yy H:mm", $Null)))){
  13. return ("Bad")
  14. }
  15. elseif((Get-date $([datetime]::ParseExact($startDate, "M/dd/yy H:mm", $Null)).ToUniversalTime() -gt $([System.DateTime]::UtcNow)){
  16. return ("Bad")
  17. }
  18. elseif((Get-date $([datetime]::ParseExact($endDate, "M/dd/yy H:mm", $Null)).ToUniversalTime() -gt $([System.DateTime]::UtcNow)){
  19. return ("Bad")
  20. }
  21. else
  22. {return ("Good")}
  23. }

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

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

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

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

As asked below copy pasting the commands ran on the console

  1. PS D:\Maintenance-Window> $startDate = '03/20/23 14:00';
  2. PS D:\Maintenance-Window> $endDate = '03/20/23 14:30';
  3. PS D:\Maintenance-Window> [ref]$parsedDate = Get-Date;
  4. PS D:\Maintenance-Window> !([DateTime]::TryParseExact($startDate, "M/d/yy H:mm", [System.Globalization.CultureInfo]::InvariantCulture,
  5. [System.Globalization.DateTimeStyles]::None,$parseddate))
  6. False;
  7. PS D:\Maintenance-Window> !([DateTime]::TryParseExact($endDate, "M/d/yy H:mm", [System.Globalization.CultureInfo]::InvariantCulture,
  8. [System.Globalization.DateTimeStyles]::None,$parseddate))
  9. False;
  10. PS D:\Maintenance-Window> !(Get-date $([datetime]::ParseExact($endDate, "M/dd/yy H:mm", $Null)) -gt
  11. (Get-Date $([datetime]::ParseExact($startDate, "M/dd/yy H:mm", $Null)))
  12. False;
  13. PS D:\Maintenance-Window> (Get-date $([datetime]::ParseExact($startDate, "M/dd/yy H:mm", $Null)).ToUniversalTime() -gt
  14. $([System.DateTime]::UtcNow)
  15. False;
  16. PS D:\Maintenance-Window> !(Get-date $([datetime]::ParseExact($startDate, "M/dd/yy H:mm", $Null)).ToUniversalTime() -gt
  17. $([System.DateTime]::UtcNow)
  18. 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

  1. function IsValidTimeStamp($startDate,$endDate)
  2. {
  3. [ref]$parsedDate = (Get-Date).ToUniversalTime()
  4. if (!([DateTime]::TryParseExact($startDate, "M/d/yy H:mm", [System.Globalization.CultureInfo]::InvariantCulture,
  5. [System.Globalization.DateTimeStyles]::None,$parseddate))){
  6. return ("Bad")
  7. }
  8. elseif(!([DateTime]::TryParseExact($endDate, "M/d/yy H:mm", [System.Globalization.CultureInfo]::InvariantCulture,
  9. [System.Globalization.DateTimeStyles]::None,$parseddate))){
  10. return ("Bad")
  11. }
  12. elseif(!((Get-date $([datetime]::ParseExact($endDate, "M/d/yy H:mm", $Null)) -gt (Get-Date $([datetime]::ParseExact($startDate, "M/d/yy H:mm", $Null)))){
  13. return ("Bad")
  14. }
  15. elseif(!((Get-date $([datetime]::ParseExact($startDate, "M/d/yy H:mm", $Null)).ToUniversalTime() -gt $([System.DateTime]::UtcNow))){
  16. return ("Bad")
  17. }
  18. elseif(!((Get-date $([datetime]::ParseExact($endDate, "M/d/yy H:mm", $Null)).ToUniversalTime() -gt $([System.DateTime]::UtcNow))){
  19. return ("Bad")
  20. }
  21. else
  22. {return ("Good")}
  23. }

(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
  1. function IsValidTimeStamp($startDate,$endDate)
  2. {
  3. [ref]$parsedDate = (Get-Date).ToUniversalTime()
  4. if (!([DateTime]::TryParseExact($startDate,"M/d/yy H:mm",[System.Globalization.CultureInfo]::InvariantCulture,
  5. [System.Globalization.DateTimeStyles]::None,$parseddate))){
  6. return ("Bad")
  7. }
  8. elseif(!([DateTime]::TryParseExact($endDate,"M/d/yy H:mm",[System.Globalization.CultureInfo]::InvariantCulture,
  9. [System.Globalization.DateTimeStyles]::None,$parseddate))){
  10. return ("Bad")
  11. }
  12. elseif(!(Get-date $([datetime]::ParseExact($endDate,"M/dd/yy H:mm",$Null))) -gt (Get-Date $([datetime]::ParseExact($startDate,"M/dd/yy H:mm",$Null)))){
  13. return ("Bad")
  14. }
  15. elseif((Get-date $([datetime]::ParseExact($startDate,"M/dd/yy H:mm",$Null))).ToUniversalTime() -gt $([System.DateTime]::UtcNow)){
  16. return ("Bad")
  17. }
  18. elseif((Get-date $([datetime]::ParseExact($endDate,"M/dd/yy H:mm",$Null))).ToUniversalTime() -gt $([System.DateTime]::UtcNow)){
  19. return ("Bad")
  20. }
  21. else
  22. {return ("Good")}
  23. }

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

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

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

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

As asked below copy pasting the commands ran on the console

  1. PS D:\Maintenance-Window> $startDate = '03/20/23 14:00'
  2. PS D:\Maintenance-Window> $endDate = '03/20/23 14:30'
  3. PS D:\Maintenance-Window> [ref]$parsedDate = Get-Date
  4. PS D:\Maintenance-Window> !([DateTime]::TryParseExact($startDate,"M/d/yy H:mm",[System.Globalization.CultureInfo]::InvariantCulture,
  5. [System.Globalization.DateTimeStyles]::None,$parseddate))
  6. False
  7. PS D:\Maintenance-Window> !([DateTime]::TryParseExact($endDate,"M/d/yy H:mm",[System.Globalization.CultureInfo]::InvariantCulture,
  8. [System.Globalization.DateTimeStyles]::None,$parseddate))
  9. False
  10. PS D:\Maintenance-Window> !(Get-date $([datetime]::ParseExact($endDate,"M/dd/yy H:mm",$Null))) -gt `
  11. (Get-Date $([datetime]::ParseExact($startDate,"M/dd/yy H:mm",$Null)))
  12. False
  13. PS D:\Maintenance-Window> (Get-date $([datetime]::ParseExact($startDate,"M/dd/yy H:mm",$Null))).ToUniversalTime() -gt `
  14. $([System.DateTime]::UtcNow)
  15. False
  16. PS D:\Maintenance-Window> !(Get-date $([datetime]::ParseExact($startDate,"M/dd/yy H:mm",$Null))).ToUniversalTime() -gt `
  17. $([System.DateTime]::UtcNow)
  18. 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

  1. function IsValidTimeStamp($startDate,$endDate)
  2. {
  3. [ref]$parsedDate = (Get-Date).ToUniversalTime()
  4. if (!([DateTime]::TryParseExact($startDate,"M/d/yy H:mm",[System.Globalization.CultureInfo]::InvariantCulture,
  5. [System.Globalization.DateTimeStyles]::None,$parseddate))){
  6. return ("Bad")
  7. }
  8. elseif(!([DateTime]::TryParseExact($endDate,"M/d/yy H:mm",[System.Globalization.CultureInfo]::InvariantCulture,
  9. [System.Globalization.DateTimeStyles]::None,$parseddate))){
  10. return ("Bad")
  11. }
  12. elseif(!((Get-date $([datetime]::ParseExact($endDate,"M/d/yy H:mm",$Null))) -gt (Get-Date $([datetime]::ParseExact($startDate,"M/d/yy H:mm",$Null))))){
  13. return ("Bad")
  14. }
  15. elseif(!((Get-date $([datetime]::ParseExact($startDate,"M/d/yy H:mm",$Null))).ToUniversalTime() -gt $([System.DateTime]::UtcNow))){
  16. return ("Bad")
  17. }
  18. elseif(!((Get-date $([datetime]::ParseExact($endDate,"M/d/yy H:mm",$Null))).ToUniversalTime() -gt $([System.DateTime]::UtcNow))){
  19. return ("Bad")
  20. }
  21. else
  22. {return ("Good")}
  23. }

答案1

得分: 1

  1. function IsValidTimeStamp($startDate, $endDate){
  2. try { (get-date) -lt $startDate -and [datetime]$startDate -lt
  3. $enddate } catch { $false } }
  4. get-date
  5. Monday, March 20, 2023 2:13:58 PM
  6. isvalidtimestamp 3/21 3/22
  7. True
  8. isvalidtimestamp 3/21 3/20
  9. False
  10. isvalidtimestamp 3/21 3/21
  11. False
  12. isvalidtimestamp 3/23 3/24
  13. True
  14. isvalidtimestamp 3/23 0/24
  15. False
英文:
  1. function IsValidTimeStamp($startDate, $endDate){
  2. try { (get-date) -lt $startDate -and [datetime]$startDate -lt
  3. $enddate } catch { $false } }
  4. get-date
  5. Monday, March 20, 2023 2:13:58 PM
  6. isvalidtimestamp 3/21 3/22
  7. True
  8. isvalidtimestamp 3/21 3/20
  9. False
  10. isvalidtimestamp 3/21 3/21
  11. False
  12. isvalidtimestamp 3/23 3/24
  13. True
  14. isvalidtimestamp 3/23 0/24
  15. 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:

确定