Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell Get-Easter function

Just to long to twitter :

Function Get-Easter ($y){$a=(19*($y%19)+15)%30;$b=(2*($y%4)+4*($y%7)-$a+34)%7;$d=(($a+$b+114)%31)+1;$m=[math]::Floor(($a+$b+114)/31);([datetime]"$m/$d/$y").adddays(13)}

can be used like this :

PS > Get-Easter 2011

Sonntag, 24. April 2011 00:00:00

*Update* : after a comment from jjen009 , that some of the dates where not correct I did a bit more research, and a there seem to be 3 different methods to calculate easter, the problem was not a bug in the function but taking the wrong method at least for western countries that is,( as I'm a not religious and originaly wanted to twitter the function, I just took the shortest ;)  ).

I did allready see 2 Columns in Wikipedia here : http://en.wikipedia.org/wiki/Easter but did not know the difference and just checked against the first one.

 But as that is not the most common one and also not the one used where I live, so not realy usefull, I also translated the Western  one ;).

function get-Easter ($year) {

    $a = $year % 19
    $b = [math]::floor($year/100)
    $c = $year % 100
    $d = [math]::floor($b/4)
    $e = $b%4
    $f = [math]::floor(($b+8)/25)
    $g = [math]::floor(($b-$f+1)/3)
    $h = (19*$a+$b-$d-$g+15)%30
    $i = [math]::floor($c/4)
    $k = $c%4
    $l = (32+2*$e+2*$i-$h-$k)%7
    $m = [math]::floor(($a+11*$h+22*$l)/451)
    $Month = [math]::floor(($h+$l-7*$m+114)/31)
    $day = (($h+$l-7*$m+114)%31)+1

    [datetime]"$Month/$Day/$Year"
}

Before making this second script ( from Wikipedia example) I found much more information about easter dates here :  http://www.gmarts.org/index.php?go=410

There is also a page with explaination about the 3 different methods and examples of different algorithms if you want to explore easter calculation yourself.

It was a much more interesting topic as I tought before, thanks jjen009 for the comment about my mistake and getting me in to it more.

* Update 2*

Got it as a Tweetliner after all by delegating the calculation  ;) :

function get-easter ($year){[datetime](New-WebServiceProxy "http://bit.ly/clujA9").easter_date($year)}

* Update 3 *

Another way witih for US and GB/I complete holiday list :

$h = New-WebServiceProxy "http://www.holidaywebservice.com/Holidays/HolidayService.asmx?WSDL";$h.GetHolidaysAvailable('US').tables | ft;$h.GetHolidayDate('US','EASTER',2011)

Enjoy, and happy easter calculating,

Greetings /\/\o\/\/

Published Sunday, November 14, 2010 7:37 AM by MoW
Filed under: , ,

Comments

# re: PowerShell Get-Easter function

Works for 2011, but it appears wrong for some years.  I think the latest date Easter can fall is something like 25 or 26 April.  Running it on dates from 2010-2010, I get:

Sunday, 4 April 2010 12:00:00 a.m.

Sunday, 24 April 2011 12:00:00 a.m.

Sunday, 15 April 2012 12:00:00 a.m.

Sunday, 5 May 2013 12:00:00 a.m.

Sunday, 20 April 2014 12:00:00 a.m.

Sunday, 12 April 2015 12:00:00 a.m.

Sunday, 1 May 2016 12:00:00 a.m.

Sunday, 16 April 2017 12:00:00 a.m.

Sunday, 8 April 2018 12:00:00 a.m.

Sunday, 28 April 2019 12:00:00 a.m.

Sunday, 19 April 2020 12:00:00 a.m.

I don't think it's possible for Easter to fall on some of these dates.

jj

Sunday, November 14, 2010 2:36 PM by

# re: PowerShell Get-Easter function

I used to have a Perl script that I wrote that did the Easter date.  I'll try to look it out and see if I can tell what is wrong with this function.

jj

Sunday, November 14, 2010 2:38 PM by

# re: PowerShell Get-Easter function

@jjen009 :

I checked the dates on Wikipedia and they look correct :

http://en.wikipedia.org/wiki/Easter

but there seems to be 2 dates easter ans western, maybe that is the problem.

Greetings MOW

Wednesday, November 17, 2010 4:20 AM by MoW
Anonymous comments are disabled