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

Viewing 4 reply threads
  • Author
    Posts
    • #165733
      Participant
      Topics: 6
      Replies: 6
      Points: 68
      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: 42
      Replies: 174
      Points: 628
      Helping Hand
      Rank: Major 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: 6
      Replies: 6
      Points: 68
      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: 2
      Replies: 1754
      Points: 3,630
      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: 6
      Replies: 6
      Points: 68
      Rank: Member

      Thanks It worked great for me

Viewing 4 reply threads
  • The topic ‘Comapring two date columns in same CSV with each other’ is closed to new replies.