Learn about Windows PowerShell
Summary: Beginner Event 7 uses Windows PowerShell to determine the number of days between dates.
About this event
Date of Event
4/12/2011 12:01 AM
4/19/2011 12:01 AM
Your company’s fiscal year ends on July 31. Recently, they implemented a “use it or lose it” budgetary policy in which budget money is not carried over from one quarter to the next. In addition, you have been told that if your department does not spend its entire budget request this year, next year’s budget will be reduced. You do not want to waste money; however, you do want to provide a reminder to people in your department as to the number of days remaining in the fiscal year. Your boss has tasked you to provide a single line of code for the departmental logon script that will display the exact number of days until the end of the fiscal year. Your company, of course, uses Windows PowerShell for the logon script. The output should appear something like the following.
There are 139 days until the end of the fiscal year.
2011 Scripting Games: All Links on One Page
Submit your scripts on PoshCode
Support our Sponsors!
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. Good luck as you compete in this year’s Scripting Games. We wish you well.
Ed Wilson, Microsoft Scripting Guy
Users should be notified when the current date is let's say 01/09/2010 and the end of fiscal year is 31/7/2011 "There are 333 days until end of the fiscal year.", or just when the current date is between 01/01/2011 and 31/07/2011 ? THX
Fiscal year and calendar year are completely separate; your "333 days" scenario should be included.
@VS I am assuming that todays date is BEFORE July 31, 2011 ... which right now it actually is. Therefore you should use todays date for the starting date, and July 31, 2011 as the ending date. Your task is to compute the number of days between today and July 31, 2011. You can hardcode the ending date. You should compute todays current date.
@j you are making it too complicated.
ouch, i also check if it´s before or after July 31st, as there was no sign that it had to be only for 2011.
Extra points if it works?
@kloinerFeigling sorry ... you are making it too hard. The scenario was about using up budget before the end of the year ... I assumed "this year" ... pay attention to the first two extra points ... it should be really simple ...
The third extra point would 'allow' you to use other dates and mesages.
1. Event Scenario asks to write a one line code.
2. Design points says extra points for resuable code.
So can we write more than 1 line code and will it be still ok?
What you see are "extra credit" suggestions. If you did a script that works that is 2 points. If you use native cmdlets and have simple code that is two more points. The remaining point will be style, approach, clarity, and if you use the simplest approach. Your solution really should be one line of code. The Other way to approach this is to write a reusable function, that exposes command line parameters, etc ... you can choose either approach, but I recommend using the one line version.