Results 1 to 8 of 8

Thread: Calculating Day of Week Given MM-DD-YYYY

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Calculating Day of Week Given MM-DD-YYYY

    I know there are probably ways to do this already, but I came up with this one all by myself.
    Attached Images Attached Images  
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    Addicted Member BobTheBuilder.'s Avatar
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    149

    Re: Calculating Day of Week Given MM-DD-YYYY

    Sounds like you want the Julian Date. Check this out.

  3. #3

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Calculating Day of Week Given MM-DD-YYYY

    I know there are other ways out there. My intention when doing this was to come up with my own formula. I didn't want to look anywhere online otherwise I wouldn't be able to figure it out by self because I would have that info in the back of my head, influencing my thought process.

    After I simplify the formula above as much as possible, I will look up Julian Dates online.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  4. #4
    Addicted Member BobTheBuilder.'s Avatar
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    149

    Re: Calculating Day of Week Given MM-DD-YYYY

    Quote Originally Posted by eyeRmonkey
    I know there are other ways out there. My intention when doing this was to come up with my own formula. I didn't want to look anywhere online otherwise I wouldn't be able to figure it out by self because I would have that info in the back of my head, influencing my thought process.

    After I simplify the formula above as much as possible, I will look up Julian Dates online.

    Oh I see. I am indeed very much impressed with that formula! I no longer have the inspiration to sit and come up with stuff like that anymore

  5. #5

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Calculating Day of Week Given MM-DD-YYYY

    I'm glad you like it. It only took me a few hours of actual thought (maybe 5?). There is a lot of binary (true/false) logic to it because I am too used to programming. Its really ugly and I am trying to simplify it.

    Here it is in VB is anyone cares:
    VB Code:
    1. Private Function Convert( _
    2.         ByVal M As Long, _
    3.         ByVal D As Long, _
    4.         ByVal Y As Long _
    5.     )
    6.     Dim lReturn         As Long
    7.     Dim lYearStart      As Long
    8.     Dim lMonthStart     As Long
    9.     Dim lMonthOffset    As Long
    10.     Dim lDay            As Long
    11.    
    12.     lYearStart = Int((Y - 1) * (365.25))
    13.     lMonthStart = (M - 1) * 30
    14.     lMonthOffset = (1) * Int(Int(M / 2) / (Int(M / 2) - 0.0001)) + _
    15.                   (-2 + (1 - Int((Y Mod 4) / ((Y Mod 4) - 0.0001)))) * Int(Int(M / 3) / (Int(M / 3) - 0.0001)) + _
    16.                    (1) * Int(Int(M / 4) / (Int(M / 4) - 0.0001)) + _
    17.                    (1) * Int(Int(M / 6) / (Int(M / 6) - 0.0001)) + _
    18.                    (1) * Int(Int(M / 8) / (Int(M / 8) - 0.0001)) + _
    19.                    (1) * Int(Int(M / 9) / (Int(M / 9) - 0.0001)) + _
    20.                    (1) * Int(Int(M / 11) / (Int(M / 11) - 0.0001))
    21.     lDay = D - 1
    22.    
    23.     lReturn = (lYearStart + lMonthStart + lMonthOffset + lDay) Mod 7
    24.    
    25.     Convert = lReturn
    26. End Function
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  6. #6

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Calculating Day of Week Given MM-DD-YYYY

    Okay, I have spent a lot of time simplifying this formula. I attached an image of the new version. This is actually the 3rd revision I have made. The last one I didn't post because it was only slightly nicer than the one above.

    Here is the code in VB.

    VB Code:
    1. Private Function ConvertToDayOfWeek3( _
    2.         ByVal M As Long, _
    3.         ByVal D As Long, _
    4.         ByVal Y As Long _
    5.     ) As Long
    6.     Dim lReturn             As Long
    7.     Dim lYearStart          As Long
    8.     Dim lMonthOffset        As Long
    9.     Dim lDay                As Long
    10.     Dim lLeapYearAfterFeb   As Long
    11.    
    12.     lYearStart = Int(365.25 * (Y - 1))
    13.     lMonthOffset = Int(530526416330# / 10 ^ (M - 1)) - Int(530526416330# / 10 ^ M) * (10)
    14.     lLeapYearAfterFeb = (1 - Int((Y Mod 4) / (Y Mod 4 - 0.01))) * Int(Int(M / 3) / (Int(M / 3) - 0.0001))
    15.     lDay = D - 1
    16.    
    17.     lReturn = (lYearStart + lMonthOffset + lDay + lLeapYearAfterFeb) Mod 7
    18.    
    19.     ConvertToDayOfWeek3 = lReturn
    20. End Function
    Attached Images Attached Images  
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  7. #7
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Calculating Day of Week Given MM-DD-YYYY

    Check this one out, at least you'll have an on-line benchmark to test your code against.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Calculating Day of Week Given MM-DD-YYYY

    Compare it to the formula shown in this thread...

    http://www.vbforums.com/showthread.p...ht=1582+julian

    One is mainframe BASIC, the other is VB - but basically the same formula...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width