PowerTip: Use PowerShell to Determine Weeks Between Dates


Summary: Use Windows PowerShell to determine the number of weeks between two dates.

Hey, Scripting Guy! Question I have a project that is broken into several major components of work. I know the start date of the project and
           the end date of the project. How can I use Windows PowerShell to determine the number of weeks between
           these dates so I can determine how many weeks to allocate for each milestone?

Hey, Scripting Guy! Answer Use the New-TimeSpan cmdlet to create a timespan object that represents the amount of time between
           the start date and the end date. Then divide the Days property by seven to determine the number of weeks.
           Here is an example:

PS C:\> $ts = New-TimeSpan -Start 1/5/2015 -End 5/25/15 

PS C:\> $ts.Days / 7

20

Comments (3)

  1. jrv says:

    Actually that may not always be accurate. Weeks are numbered in strange ways.

    (get-date ‘5/25/15’ -uformat %V) – (get-date ‘1/5/2015’ -uformat %V)

    There is another major week method used by many retailers. It is skewed by a few days and is used to calculate sales by salaries or some such nonsense. It varies by the day of the week for January 1 because of that being a big sales season. I don’t have a good
    spec but had to write around it once.

  2. Valeras says:

    thanks jrv,
    your line works better if time is for 2016:

    PS C:> $ts = New-TimeSpan -Start 1/5/2016-End 5/25/16

    PS C:> $ts.Days / 7
    result: 20.1428571428571

    using jrv its 20

  3. calin says:

    $ts = New-TimeSpan -Start ([datetime]"1/5/2015") -End ([datetime]"5/25/15")
    $ts.Days / 7

    on my powershell work like this

Skip to main content