Comapring two date columns in same CSV with each other

Hello Everyone,

I have a CSV file with two date columns dd/mm/yyyy format, I am trying to compare dates in those two column with each other and find out difference in third columns in number of days. Right now I am thinking of start like this but this does not helping me much.

$dates = Import-csv C:\Users\jhajja\Documents\Script\Result1.csv

$dates | select-object -property * | Compare-object -ReferenceObject Column 1 -DifferenceObject Column 2





Hopefully I’ve understood the question correctly…

$dates = import-csv "C:\Users\jhajja\Documents\Script\Result1.csv"
foreach($Date in $Dates) {
$Days = (New-TimeSpan -Start $Date.startdate -End $Date.enddate).Days
}|Export-Csv "C:\Users\jhajja\Documents\Script\NewResults.csv" -NoTypeInformation

Thanks Iain, The following code returning me result from only last row in CSV, not for the first 15 rows just for 16th one. Any suggestions on how to fix it.

I assume that should be just an example of how you could approach this task. Actually you did not provide enough information.

I do further assume that your CSV file has headers - so you should exchange the header names Column1 and Column2 in this snippet with the header names of your CSV file.

Import-Csv -Path ‘C:\Users\jhajja\Documents\Script\Result1.csv’ |
Select-Object -Property *,@{Name=‘TimeDifference’;Expression={(New-TimeSpan -Start $.Column1 -End $.Column2).Days }}

If your CSV file does not have headers you should add them along with the Import-Csv cmdlet. And of course you would need to provide the -Delimiter if you do not use the standard.

Thanks It worked great for me