Fiddle Freak
Fiddle Freak

Reputation: 2041

Compare dates with different formats in csv file

Whats a good way to compare dates in a csv file that looks like this:

Date1,Date2,Date3
11/10/2016  9:45:00 PM,20161110,11/10/2016
11/15/2016  11:24:00 PM,20160924,11/10/2016

If a match is found, append a column like so...

Date1,Date2,Date3,MatchDates
11/10/2016  9:45:00 PM,20161110,11/10/2016,Match Found
11/15/2016  11:24:00 PM,20160924,11/10/2016,No Match Found

updated

Trying the code that is in the comments:

When comparing this with 2 of the columns...

$csvFile = 'C:\Scripts\Tests\test1.csv'

Import-Csv $csvFile | Select-Object *, @{n='MatchDates';e={
if(([datetime]$_.Date1).Date -eq $_.Date3){
'Match Found'
}Else{
'No Match Found'
}}} | 
    Export-Csv "$csvFile-results.csv" -NoTypeInformation -Force

output (isCorrect)...

Date1,Date2,Date3,MatchDates
11/10/2016  9:45:00 PM,20161110,11/10/2016,Match Found
11/15/2016  11:24:00 PM,20160924,11/10/2016,No Match Found

However, if I try to compare all 3 columns using the following code

Import-Csv $csvFile | Select-Object *, @{n='MatchDates';e={
if((([datetime]$_.Date1).Date -eq $_.Date3) -and (([datetime]$_.Date2).Date -eq $_.Date3) -and (([datetime]$_.Date1).Date -eq $_.Date2)){
'Match Found'
}Else{
'No Match Found'
}}} | 
    Export-Csv "$csvFile-results.csv" -NoTypeInformation -Force

output (isNotCorrect)...

Date1,Date2,Date3,MatchDates
11/10/2016  9:45:00 PM,20161110,11/10/2016,
11/15/2016  11:24:00 PM,20160924,11/10/2016,No Match Found

As you can see the value of row 1 and the last column is $null instead of showing Match Found

Maybe I'm not understanding something correctly?

Upvotes: 0

Views: 887

Answers (1)

Matt
Matt

Reputation: 46710

You were on the right track with what we discussed in comments. Problem was with that middle date. It does not convert to a [datetime] without some help. That is where ParseExact comes in handy. Consider the following:

PS D:\temp> [datetime]"20160924"
Cannot convert value "20160924" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."
At line:1 char:1
+ [datetime]"20160924"
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider


PS D:\temp> [datetime]::parseexact("20160924","yyyyMMdd",[System.Globalization.CultureInfo]::CurrentCulture)

Saturday, September 24, 2016 12:00:00 AM

Note the second example returned a proper date object.


Do you remember the transitive property from math? We use that for simple comparison to see if all dates are the same. Not the only way by far but a simple one nonetheless. Building off your calculated property code

$csv |  Select-Object *,@{Name='MatchDates';Expression={
        $date1 = ([datetime]$_.Date1).Date
        $date2 = ([datetime]::parseexact($_.Date2,"yyyyMMdd",[System.Globalization.CultureInfo]::CurrentCulture)).Date
        $date3 = ([datetime]$_.Date1).Date
        if($date1 -eq $date2 -and $date2 -eq $date3){
            'Match Found'
        } else {
            'No Match Found'
        }
    }
}

Cleared up the if logic by saving the casted values in temproary variables.

Upvotes: 1

Related Questions