PowerTip: Get Time Difference between Dates with PowerShell

Summary: Use New-TimeSpan to produce the time difference between two dates.

Hey, Scripting Guy! Question I know I can subtract dates, but is there a Windows PoweShell cmdlet to show me the time difference between dates?

Hey, Scripting Guy! Answer Use New-TimeSpan and supply the information!
          For example, to show the difference between today and the upcoming New Year:


$EndDate=[datetime]”01/01/2014 00:00”

NEW-TIMESPAN –Start $StartDate –End $EndDate

Comments (13)

  1. Chen V says:

    Very much required for all the administrators!!!

  2. @Greg Wohan

    You are correct. There are two ways in PowerShell to do the same exact thing. Neither is wrong. One uses a Cmdlet and one uses Math (Although I suspect the Cmdlet is just coughing in the math). Choose whichever makes more sense for you to do 🙂


  3. Greg Wojan says:

    Why not just subtract the dates and let PowerShell do the conversion for you? The results are the same just a little less explicit.

    PS >($EndDate – $StartDate).gettype().Name

  4. Anonymous says:

    $StartDate is not needed here: PS will use current date for either of two parameters, if it’s not explicitly specified. So New-TimeSpan -End 1-Jan-2014 would probably do. đź?‰

  5. Jørgen E. Nielsen says:

    Please use day-numbers above 12 in examples, to prevent confusion between day and month position.

  6. Derry says:

    Hey Guys – I need to generate a script that will search a folder structure for old files. I want the user to input the folder location (d:folder) and a date (01/01/2008) for instance. What I want returned is a list of files that meet that criteria –
    i.e. older than 01/01/2008.

    I had this script created which goes on DAYS you want to search for, I want the user to input a date instead and perform a calculcation against the current date. The second one is not working – getting errors. Pretty new to PowerShell so any advice would be

    Do you guys recommend using -lt or -le or -gt or -ge

    ********* First *********
    Write-host "Please enter folder location you want to search e.g. c:windows"
    $Dir = read-host
    Write-host "Files that HAVE been modified in the past number of days?"
    $Days = read-host
    get-childitem -Path $Dir –recurse |
    where-object {$_.lastwritetime -lt (get-date).addDays(-$Days)} |
    Foreach-Object { $_.FullName }

    ********* Second *********
    Write-host "Please enter folder location you want to search e.g. c:windows"
    $Dir = read-host
    $Date = Read-Host "Enter Date that Files have NOT been modified"
    if (($Date -as [DateTime]) -ne $null) {
    $Date = [DateTime]::Parse($Date)
    } else {
    ‘You did not enter a valid date!’
    $NewDays = ($Date-(get-date))
    get-childitem -Path $Dir –recurse |
    where-object {$_.lastwritetime -lt (get-date).addDays($NewDays)} |
    Foreach-Object { $_.FullName }

  7. arun says:

    Hi All,

    we can use windows 7 calculator also to do this



  8. Shawn Kovac says:

    I found a 3rd way that seems to be shorter in typing *and* does not use a different date format based on culture. I use the Subtract method of the DateTime object and the constructor of the DateTime object:
    (get-date).date.subtract((new-object DateTime(2015,7,5)))
    or for the example in the article it wud be more like this:
    (new-object DateTime(2016, 1, 1)).Subtract((get-date))
    Using DateTime’s constructor (with PowerShell’s New-Object Cmdlet) is the most elegant way i’ve found to create custom dates. Ambiguous parsing based on culture is so fundamentally a bad idea that i’m surprised everyone doesn’t see it as a bad practice. But
    apparently enuf people do see it since i see at least one comment on every thread i’ve read about DateTime handling in PowerShell. it *is* important for bug sakes, my fellow developers.

  9. Shawn Kovac says:

    For Jørgen E. Nielsen, i ammend my contribution to:
    (get-date).date.subtract((new-object DateTime(2015, 7, 15)))
    (new-object DateTime(2016, 1, 15)).Subtract((get-date))
    This way by reading the chosen example dates, anyone can deduce the year, month, day order.

  10. Avi Zadok says:

    Thank you 🙂

  11. Paul Perry says:

    FYI, those who’ve not worked with timespan before (*raises hand*), you can do the following to get any of the entries, for example, if all I want is the number of days I could use:

    ($EndDate – $StartDate).Days


    (NEW-TIMESPAN –Start $StartDate –End $EndDate).Days

    Hope that helps save somebody time (no pun intended) 🙂

Skip to main content