PowerTip: Use PowerShell to Show How Many Days Until the End of the Year


Summary: Learn how to use the New-TimeSpan cmdlet to see the number of days until the end of the year.

Hey, Scripting Guy! Question How can I use Windows PowerShell to see the number of days until the end of the year?

                       Hey, Scripting Guy! Answer Use the New-TimeSpan cmdlet. Use Get-Date for the start and the day you wish for the endThe following shows this technique by using a U.S. English cultural setting.

                                             New-TimeSpan -Start (get-date) -End 12/31/2012

 

Comments (9)

  1. Thomas Ertl says:

    Great Answer !

    A simple solution, but … 31/12/2012 works better.

  2. Anonymous says:

    Good one.

  3. Jaap Brasser says:

    If you use ISO notation it is also independent of regional settings:

    New-TimeSpan -Start (Get-Date) -End 2012/12/31

  4. siodmy says:

    New-TimeSpan -Start (get-date) -End (Get-Date -Day 31 -Month 12 -Year (Get-Date).Year) | select -ExpandProperty TotalDays

    Works every year, for every system locale (sorry, I couldn't leave it like that).

  5. conrad braam says:

    Any tips on an internationalazed version of this question? But much more helpful would be a answer that works for any year. Is this possible?

  6. conrad braam says:

    Thanks siodmy, great one-lineable answer. Sorry I read these on mobile.

  7. Michael Menzies says:

    Using 1/1/2013 would fix usuk date format and fix the day early since the time defaults to 0:00:00am not 11:59:59pm

  8. Ed Wilson says:

    @Thomas Ertl thanks 🙂

    @siodmy hmmmm I did not think about that one. Great answer!

    @Conrad braam I have a whole series of Hey Scripting Guys blog posts where I talk about detecting and selecting specific locale settings. Here are a few articles to get you started:

    social.technet.microsoft.com/…/en-US

  9. Ed Wilson says:

    @Jaap Brasser, yes that is a good point

    @Michael Menzies cool … I did not know that.

Skip to main content