title: Getting the # of days difference between two dates in Powershell
body: <p>I am trying to get the number of days difference in Windows powershell, I am extracting the last date of the year i.e.. <code>20171231(yyyyMMdd)</code> from a text file that I have locally stored the date in that file.</p>
  <p>Here is the below code that I am trying but not able to get the difference of the days, am getting the wrong output by directly subtracting, if am converting the string extracted from the file and then subtract it with the date type, even then am getting the wrong output.</p>
  <pre class="lang-sh prettyprint-override"><code>$DateStr = (Get-Date).ToString(&quot;yyyyMMdd&quot;)
  
  $content = Get-Content C:\Users\Date.txt 
  
  $diff = $content.ToString();
  
  #Convert it to the date type
  $diff = [datetime]::parseexact($diff, 'yyyyMMdd', $null)
  
  #$diff3 = $diff - $DateStr
  </code></pre>