PowerTip: Get Time Difference between Dates with PowerShell

PowerTip: Get Time Difference between Dates with PowerShell

  • Comments 20
  • Likes

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Very much required for all the administrators!!!

  • Nice Article.

  • 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 TimeSpan

  • @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 :) Sean

  • $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. ;)

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

  • 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 great.

    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 }

  • http://xkcd.com/1179/

  • Hi All,

    we can use windows 7 calculator also to do this



  • uber gratis on http://www.ubergratis.com
    uber coupon code on http://www.ubergratis.com

  • Jadwal bola hari ini on http://www.hantubola.com
    Prediksi bola malam ini on http://wwwhantubola.com
    Prediksi bola akurat on http://wwwhantubola.com

  • 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.

  • 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.