Function to return date of Easter for the given year

前端 未结 8 1424
眼角桃花
眼角桃花 2020-12-08 05:12

So, here\'s a funny little programming challenge. I was writing a quick method to determine all the market holidays for a particular year, and then I started reading about E

8条回答
  •  Happy的楠姐
    2020-12-08 05:37

    The below code determines Easter through powershell:

    function Get-DateOfEaster {
        param(
            [Parameter(ValueFromPipeline)]
            $theYear=(Get-Date).Year
            )
    
        if($theYear -lt 1583) {
            return $null
        } else {
    
            # Step 1: Divide the theYear by 19 and store the
            # remainder in variable A.  Example: If the theYear
            # is 2000, then A is initialized to 5.
    
            $a = $theYear % 19
    
            # Step 2: Divide the theYear by 100.  Store the integer
            # result in B and the remainder in C.
    
            $c = $theYear % 100
            $b = ($theYear -$c) / 100
    
            # Step 3: Divide B (calculated above).  Store the
            # integer result in D and the remainder in E.
    
            $e = $b % 4
            $d = ($b - $e) / 4
    
            # Step 4: Divide (b+8)/25 and store the integer
            # portion of the result in F.
    
            $f = [math]::floor(($b + 8) / 25)
    
            # Step 5: Divide (b-f+1)/3 and store the integer
            # portion of the result in G.
    
            $g = [math]::floor(($b - $f + 1) / 3)
    
            # Step 6: Divide (19a+b-d-g+15)/30 and store the
            # remainder of the result in H.
    
            $h = (19 * $a + $b - $d - $g + 15) % 30
    
            # Step 7: Divide C by 4.  Store the integer result
            # in I and the remainder in K.
    
            $k = $c % 4
            $i = ($c - $k) / 4
    
            # Step 8: Divide (32+2e+2i-h-k) by 7.  Store the
            # remainder of the result in L.
    
            $l = (32 + 2 * $e + 2 * $i - $h - $k) % 7
    
            # Step 9: Divide (a + 11h + 22l) by 451 and
            # store the integer portion of the result in M.
    
            $m = [math]::floor(($a + 11 * $h + 22 * $l) / 451)
    
            # Step 10: Divide (h + l - 7m + 114) by 31.  Store
            # the integer portion of the result in N and the
            # remainder in P.
    
            $p = ($h + $l - 7 * $m + 114) % 31
            $n = (($h + $l - 7 * $m + 114) - $p) / 31
    
            # At this point p+1 is the day on which Easter falls.
            # n is 3 for March and 4 for April.
    
            $DateTime = New-Object DateTime $theyear, $n, ($p+1), 0, 0, 0, ([DateTimeKind]::Utc)
            return $DateTime
        }
    }
    
    $eastersunday=Get-DateOfEaster 2015
    Write-Host $eastersunday
    

提交回复
热议问题