Link to home
Start Free TrialLog in
Avatar of bibi92
bibi92Flag for France

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

$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 }

Open in new window


Avatar of ste5an
ste5an
Flag of Germany image

I don't understand the question. Can you rephrase it and add more context and sample data?

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.
I tend to agree with ste5an.

To do what you want, you have to specify a culture reference to ParseExact().
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial