Avatar of bibi92
bibi92
Flag 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


Powershell

Avatar of undefined
Last Comment
oBdA

8/22/2022 - Mon
ste5an

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.
Michael B. Smith

I tend to agree with ste5an.

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

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.
fblack61