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

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

  • Comments 9
  • Likes

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

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Great Answer !

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

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

  • Good one.

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

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

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

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

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

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

  • @Jaap Brasser, yes that is a good point

    @Michael Menzies cool ... I did not know that.