bibi92
asked on
problem date format
Hello,
How to modify the date format on the script depending fr or Us for resolve :
Could not compare "06/04/2021 11:41:15" to "30/05/2021 06:06:16". Error: "Cannot convert value "30/05/2021 06:06:16"
to type "System.DateTime". Error: "String was not recognized as a valid DateTime.""
Thank you
How to modify the date format on the script depending fr or Us for resolve :
Could not compare "06/04/2021 11:41:15" to "30/05/2021 06:06:16". Error: "Cannot convert value "30/05/2021 06:06:16"
to type "System.DateTime". Error: "String was not recognized as a valid DateTime.""
Thank you
$Days = 3
$TimeFR = (Get-Date).AddDays(-$Days).ToString("dd-MM-yyyy hh:mm:ss")
$TimeUS = (Get-Date).AddDays(-$Days).ToString("MM-dd-yyyy hh:mm:ss")
$FormatUS = 'MM/dd/yyyy HH:mm:ss'
$FormatFR = 'dd/MM/yyyy HH:mm:ss'
$LogPath = "C:\TEMP\Log"
$Log = Get-ChildItem -path $LogPath -recurse -include dly_test.log
$ListError = 'ERR1014','ERR1016','ERR1012','ERR1011','ERR1013'
If ( $value -eq 'computer')
{
$Format = $FormatFR
$ReferenceTime = $ReferenceTimeFR
}
Else
{
$Format = $FormatUS
$Time = $ReferenceTimeUS
}
$Errorz = Get-Content -Path $Log |
Where-Object { $_ -match 'ERR1013' -and [datetime]::ParseExact((($_ -split ' ')[0] + " " + ($_ -split ' ')[1]), $Format, $null) -gt $Time }
I tend to agree with ste5an.
To do what you want, you have to specify a culture reference to ParseExact().
To do what you want, you have to specify a culture reference to ParseExact().
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
But I have to admit that I don't understand the problem. There is only one valid format, which is ISO 8601 to avoid any ambiguity and to add lexical sortablilty as property to an date/time literal.
Thus my first step is always to get the best available input.
Especially when it is about date formats, using ambiguous date formats will always lead to lesser data quality.