/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" Download PowerShell 1.0 RTM "," PowerShell goes RTM "," Windows PowerShell Week on ScriptCenter "," PowerShell : How Can I Split a String Only on Spec... "," PowerShell : How Can I Tell Whether a Number is Ev... "," PowerShell : 99-bottles-of-beer "," PowerShell : Tabcompletion Part 5 "," PowerShell : Calendar Function (GUI) "," PowerShell : WMI Support in RC2 : Privileges and c... "," PowerShell : WMI Support in RC2 (Series part 2) "," ")

Friday, November 24, 2006


PowerShell : Playing with LeapYears


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 :



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

PoSH>[datetime]::IsLeapYear((read-host "Year"))
Year: 8

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)}
Year: 2008
2008 = Leapyear : True
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
PoSH>(get-date -year 2012).year -lt (get-date).year

below 30 use 2000 rangle


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"}}}
PoSH>switch ((get-date -year 2006).year - (get-date).year){0 {"is"} default {if ($_ -gt 0){"will be"} else {"was"}}}


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(
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.


Tags : Monad PowerShell

Anonymous Anonymous
A better verb-noun combination would be Test-LeapYear. You aren't actualy getting a leap year, you are testing to see if it is a leap year or not.
Blogger /\/\o\/\/
You are right :-)

Greetings /\/\o\/\/
Post a Comment

Links to this post:

Create a Link

<< Home


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  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?