On Rob van der Woude's Scripting Pages, there is an excelent PowerShell section,
with some good links and some examples.
one of the examples is how to test a year if it is a leap year.
this script did look a bit for such a trivial task in powerShell, as I did Know that the .NET [dateTime] class did have some handy methods, I started out like this :
PoSH>[datetime]::IsLeapYear(2006)
False
PoSH>[datetime]::IsLeapYear(2008)
True
but ofcourse this was not the only thing the script did do if you look at the original script we just replaced one line
# Check if the specified year is a leap year
[boolean]$leapyear = ( [boolean]!( $year % 4 ) -and [boolean]( $year % 100 ) ) -or [boolean]!( $year % 400 )
Read some info from the host
PoSH>[datetime]::IsLeapYear((read-host "Year:"))
Year:: 2006
FalsePoSH>[datetime]::IsLeapYear((read-host "Year"))
Year: 8
True
Add some formatting
PoSH>function get-leapYear ($year = (read-host "Year")){"$year = Leapyear : {0}" -f [datetime]::IsLeapYear($Year)}
PoSH>get-leapyear 2008
2008 = Leapyear : True
Combine asking the year and providing default value
PoSH>function get-leapYear ($year = (read-host "Year")) {if(! $year) {$year = (get-date).year} ; "$year = Leapyear : {0}
" -f [datetime]::IsLeapYear($Year)}
PoSH>get-leapYear
Year: 2008
2008 = Leapyear : True
PoSH>get-leapYear
Year:
2006 = Leapyear : False
PoSH>get-leapYear 12
12 = Leapyear : True
Gotha with 2 digit years
PoSH>(get-date -year 12).year -lt (get-date).year
True
PoSH>(get-date -year 2012).year -lt (get-date).year
False
below 30 use 2000 rangle
PoSH>[datetime]::ParseExact("12","yy",$null).year
2012
so to solve that :
PoSH>function get-leapYear ($year = (read-host "Year")) {if(! $year) {$year = (get-date).year} ; if ($year -lt 100){$yea
r = [datetime]::ParseExact($year,"yy",$null).year};"$year = Leapyear : {0}" -f [datetime]::IsLeapYear($Year)}
PoSH>get-leapYear 12
2012 = Leapyear : True
Formatting of the past, present or future
PoSH>switch ((get-date -year 2007).year - (get-date).year){0 {"is"} default {if ($_ -gt 0){"will be"} else {"was"}}}
will be
PoSH>switch ((get-date -year 2005).year - (get-date).year){0 {"is"} default {if ($_ -gt 0){"will be"} else {"was"}}}
was
PoSH>switch ((get-date -year 2006).year - (get-date).year){0 {"is"} default {if ($_ -gt 0){"will be"} else {"was"}}}
is
So complete :
PoSH>function get-leapYear ($year = (read-host "Year")) {if(! $year) {$year = (get-date).year} ; if ($year -lt 100){$yea
r = [datetime]::ParseExact($year,"yy",$null).year};"$year {0} a Leap Year" -f $(switch ((get-date -year $year).year - (g
et-date).year){0 {"is {0}"} default {if ($_ -gt 0){"will {0} be"} else {"was {0}"}}}) -f $(if ( ![datetime]::IsLeapYear(
$Year)){"NOT"})}
PoSH>get-leapYear 2005
2005 was NOT a Leap Year
PoSH>get-leapYear 2006
2006 is NOT a Leap Year
PoSH>>get-leapYear 2007
2007 will NOT be a Leap Year
PoSH>get-leapYear 2008
2008 will be a Leap Year
PoSH>get-leapYear 12
2012 will be a Leap Year
So you can see the sample was not that bad except for not using the [dateTime]::IsLeapYear() function .
but I hope you see from above examples how flexible you can work in PoSH, bu handling the input and formatting in different ways as I tested this all as onliners interactively.
now ofcourse some whitespace an variables would be handy.
Enjoy,
Greetings,
/\/\o\/\/
Tags : Monad PowerShell
October 2005 November 2005 December 2005 January 2006 February 2006 March 2006 April 2006 May 2006 June 2006 July 2006 August 2006 September 2006 October 2006 November 2006 December 2006