PowerTip: Get Time Difference between Dates with PowerShell

PowerTip: Get Time Difference between Dates with PowerShell

  • Comments 16
  • 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:

$StartDate=(GET-DATE)

$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
    Write-host
    $Date = Read-Host "Enter Date that Files have NOT been modified"
    if (($Date -as [DateTime]) -ne $null) {
    $Date = [DateTime]::Parse($Date)
    $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

    https://www.youtube.com/watch?v=EbNk0ycgOQ8

    Thanks,
    Arun