Jump to content

Calculating the day of the week


NiM
 Share

Recommended Posts

I could not find something to do this, though I did not look very much (couldn't think of any key words to search for), so I made my own. Not sure if it works 100%, but I tested it on the dates from the wiki I used to make it, and on a few leap year dates, and it hasn't failed me yet.

I used http://en.wikipedia.org/wiki/Calculating_t...day_of_the_week to get the algorithm

It returns the day number which corresponds to the $days array (how they had in the wiki), so you just pop the output into $days[$output] and it gives you the day, I couldn't decided how I wanted to return the value (have the func return something fancy or some other fancy way) so I just did it the way I did.

Also in line 28

$y = int($year/4);+$year
If anyone can figure out why it wanted me to add the year, I never seemed to need to, and their explanation was hard to understand for the most part.

Global Const $days[7] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

Global Const $leapyear[2] = [6, 2];handles the leap year
Global Const $monthstable [12] = [   _ 
                                    0, _;January      0 (in leap year 6)
                                    3, _;February    3 (in leap year 2)
                                    3, _;March      3
                                    6, _;April      6
                                    1, _;May          1
                                    4, _;June        4
                                    6, _;July        6
                                    2, _;August    2
                                    5, _;September  5
                                    0, _;October      0
                                    3, _;November    3
                                    5  _;December    5 
                                 ]

$day = Day_of_Week(2008, 10, 6)
MsgBox(0, $day, $days[$day])

Func Day_of_Week($century, $month, $dayofmonth)
    $month -= 1
;http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week#An_algorithm_to_calculate_the_day_of_the_week
    $cent = StringLeft($century, 2)
    $c = 2*(3-(mod($cent, 4)))
;~  MsgBox(0, "$c", $c)

    $year = StringRight($century, 2)
    $y = int($year/4);+$year;the page had +$year but it doesnt seem to get used, even when they show their examples
;~  MsgBox(0, "$y", $y)

    If _DateIsLeapYear($century) = 1 AND $month = 0 OR $month = 1 then 
        $day = Mod($c+$year+$y+$leapyear[$month]+$dayofmonth, 7)
        ConsoleWrite("yes")
    Else
        $day = Mod($c+$year+$y+$monthstable[$month]+$dayofmonth, 7)
        ConsoleWrite("no")
    EndIf

    return $day;do i wana return this, or do something fancy? so it returns both or something
EndFunc;==>Day_of_Week

;===============================================================================
;
; Description:    Returns 1 if the specified year falls on a leap year and
;                  returns 0 if it does not.
; Parameter(s):  $iYear - Year to check
; Requirement(s):   None
; Return Value(s):  On Success - 0 = Year is not a leap year
;                               1 = Year is a leap year
;                  On Failure - 0 and sets @ERROR = 1
; Author(s):        Jeremy Landes <jlandes at landeserve dot com>
; Note(s):        None
;
;===============================================================================
Func _DateIsLeapYear($iYear)
    If StringIsInt($iYear) Then
        Select
            Case Mod($iYear, 4) = 0 And Mod($iYear, 100) <> 0
                Return 1
            Case Mod($iYear, 400) = 0
                Return 1
            Case Else
                Return 0
        EndSelect
    Else
        SetError(1)
        Return 0
    EndIf
EndFunc  ;==>_DateIsLeapYear
Link to comment
Share on other sites

Help File quote:

_DateToDayOfWeek

--------------------------------------------------------------------------------

Returns the weekdaynumber for a given date.

#Include <Date.au3>

_DateToDayOfWeek($iYear, $iMonth, $iDay)

Parameters

$iYear A valid year in format YYYY

$iMonth A valid month in format MM

$iDay A valid day in format DD

Return Value

Success: Returns Day of the Week Range is 1 to 7 where 1=Sunday.

Failure: 0 and Set @ERROR to:

0 - No error.

1 - Invalid Input Date

You obviously forgot that the first place to search is the help file :P

Even if it is not an example script it helped you improve your coding abilities :(

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Help File quote:

You obviously forgot that the first place to search is the help file :P

Even if it is not an example script it helped you improve your coding abilities :(

I searched it, my help file might be out of date.

EDIT*** Well, I am blind, it's there, how could I miss that... /facepalm

EDIT2*** Well I am blind, but this func doesn't seem to go past the year 3000, it goes up to 2999 but not 3000 =/ AutoIt! what will you do when its past the year 3000!?

Edited by NiM
Link to comment
Share on other sites

I searched it, my help file might be out of date.

EDIT*** Well, I am blind, it's there, how could I miss that... /facepalm

EDIT2*** Well I am blind, but this func doesn't seem to go past the year 3000, it goes up to 2999 but not 3000 =/ AutoIt! what will you do when its past the year 3000!?

my guess would be, that he is lying still ... somewhere 6 feet underground ghe ghe :P

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...