英文:
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
- Start Time and End Time entered is correct (not junk)
- End Time is bigger than Start Time
- End Time is larger than current Time in UTC
- 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
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
- Start Time and End Time entered is correct (not junk)
- End Time is bigger than Start Time
- End Time is larger than current Time in UTC
- 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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论