Comapring two date columns in same CSV with each other

Welcome Forums General PowerShell Q&A Comapring two date columns in same CSV with each other

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
2 months, 1 week ago.

  • Author
    Posts
  • #165733

    Participant
    Topics: 5
    Replies: 5
    Points: 54
    Rank: Member

    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

     

    Thanks

     

     

  • #165736

    Participant
    Topics: 37
    Replies: 155
    Points: 479
    Helping Hand
    Rank: Contributor

    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
    [PSCustomObject]@{
    StartDate=$Date.startdate
    EndDate=$Date.enddate
    DaysBetween=$Days
    }|Export-Csv "C:\Users\jhajja\Documents\Script\NewResults.csv" -NoTypeInformation
    }
  • #165820

    Participant
    Topics: 5
    Replies: 5
    Points: 54
    Rank: Member

    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.

  • #165850

    Participant
    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

    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.

  • #166036

    Participant
    Topics: 5
    Replies: 5
    Points: 54
    Rank: Member

    Thanks It worked great for me

You must be logged in to reply to this topic.