2

I cannot get the time difference from a certain date using Powershell like below:

$Date = '11/12/2001'
(New-TimeSpan -Start (Get-Date -Date $Date) -End (Get-Date)).ToString("'yyyy' Years 'MM' Months 'dd' Days 'hh' Hours 'mm' Minutes 'ss' Seconds'")

Error:

Exception calling "ToString" with "1" argument(s): "Input string was not in a correct format." 
At line:2 char:1 
+ (New-TimeSpan -Start (Get-Date -Date $Date) -End (Get-Date)).ToString ... 

Why I cannot get the Years and Months elapsed?

Senior Systems Engineer
  • 1,335
  • 2
  • 37
  • 73

1 Answers1

6

TimeSpan doesn't have years or months.

$Date = "11/12/2001"  
$TimeSpan = (New-TimeSpan -Start (Get-Date -Date $Date)  
Write-Output "$($TimeSpan.ToString("dddd")) Days $($TimeSpan.ToString("hh")) Hours $($TimeSpan.ToString("mm")) Minutes $($TimeSpan.ToString("ss")) Seconds"  

6871 Days 10 Hours 30 Minutes 18 Seconds

Greg Askew
  • 39,132