www.webdeveloper.com
Results 1 to 8 of 8

Thread: Calculate Day (as in Monday or Tuesday etc) from Date

  1. #1
    Join Date
    Apr 2013
    Posts
    7

    Calculate Day (as in Monday or Tuesday etc) from Date

    Calculate Day (as in Monday or Tuesday etc) from Date

    I have a value in a textbox (date) ex: 04/22/2013 2:51:00 PM
    Can I calculate from this value the day of the week?
    Any examples?
    Thanks

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Lightbulb

    One of many ways:
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
    <input type="text" id="DateEntry" value="04/22/2013 2:51:00 PM ">
    <button onclick="dayOfWeek(document.getElementById('DateEntry').value)">Day of Week</button>
    
    <script type="text/javascript">
    function dayOfWeek(info) {
      var DOW = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
      tarr = info.split(' ');
      darr = tarr[0].split('/');
      var tdate = new Date(darr[2],darr[0]-1,darr[1]);
      alert('Day of week is: '+tdate.getDay()+'\n otherwise known as: '+DOW[tdate.getDay()]);
    }
    </script>
    
    </body>
    </html>

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    An other natural method only for information...
    Code:
    var dat="04/22/2013 2:51:00 PM";
    var j=dat.replace(/0?(\d+)\/0?(\d+)\/(\d\d)(\d\d)[^\/]+/,function(all,m,j,s,a){if (m<3){m+12;a=a-1}
    	return (Math.floor(2.6*m+2.7)+Math.floor(5*a/4)+(3-s%4)<<1)%7;});
    alert(['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'][j])
    But the best method is to calculate the days without machine in less than 10 seconds !

    1/ Retains 6,4,2,0 for 1600-1799,1700-1899,1800-1899,1900-1999 and 6,4,2,0 for 2000-2100... => 6 !
    Take the two last digits of the year 13, add his quarter 3 and retains the remainder modulo 7 : 13+3 => 16 => 2 (+6 => 8 =>) 1
    2/ Add 033 614 625 035 for jan,fev,mar, apr,may,jun, jul,aug,sep, oct,nov,dec. Then 6 (+1 => 7 =>) 0

    3/ Add the date 22 =>1 It's a Monday !

    An unique exception : subtract 1 in January and February of leap year (the 29th February has been count with the quarter).

    This method was made for the preceding century (0 for 1990 : the first January 1900 was a Monday).
    It is still very convenient for birthdays...
    You have only to retains 033 614 625 035 witch are the gap from preceding months (3 in February for the 31=28+3 days in January, nor 3 in March with the 28=4x7 days in February...)
    Last edited by 007Julien; 04-22-2013 at 07:47 PM.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434
    Wow! Where in the world did you find that formula???

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    I find that formula when I was teenager...

    It's very simple. Each year (non leap year) is 365 days or 52 weeks plus one day, then each year gives a gap from one day. The leap years (after 1900 since 2099) are evenly spaced every 4 years...

    Some examples :
    D-Day : June 6, 1944 => (44+11=55=>6) (June=>4) (6th=>6) => 6+4+6=16=>2 a Tuesday !
    Assassination of John F. Kennedy : November 22, 1963 (63=>0+15=>1) (Nov=>3) (22=>1) 5 a Friday !
    Coronation of Queen Elizabeth II : June, 2 1953 (53=>4+13=>6,4+6=10=>3) (June=>4) (2=2) 2 a Tuesday !
    MoonWalk Neil Amstrong , Buzz Aldrin : July 21, 1969, (69=>6,17=>3,6+3=9=>2) (July=>6) (21=>0) 1 Obviously a Monday !
    Last edited by 007Julien; 04-23-2013 at 05:19 AM.

  6. #6
    Join Date
    Feb 2006
    Posts
    2,927
    // I prefer to let the machine do the work-

    Code:
    function dowFromDmy(input){
        var M= input.value.match(/(\d+)/g);
        if(M){
            return 'Sun,Mon,Tues,Wednes,Thurs,Fri,Sat'.split(',')[
            new Date(+M[2], M[0]-1, +M[1]).getDay()]+ 'day';
        }
        return null;
    }
    var input={value:'04/26/2013 2:51:00 PM '}; // fake input field
    dowFromDmy(input);

    /* returned value: (String)
    Friday
    */

  7. #7
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374
    Or let the computer do even more work:
    Code:
    var dayname = new Array ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
    ......................................
      x = date1.getTime() / 2 + date2.getTime() / 2;  
      date3 = new Date(x);
      var d = date3.getDay();
      document.form1.time3.value = date3.toString() + " " + dayname[d];
    This example is adapted from a midpoint between two times script, but I don't have the day of week feature included.

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Thumbs up

    Quote Originally Posted by mrhoo View Post
    // I prefer to let the machine do the work-

    Code:
    function dowFromDmy(input){
        var M= input.value.match(/(\d+)/g);
        if(M){
            return 'Sun,Mon,Tues,Wednes,Thurs,Fri,Sat'.split(',')[
            new Date(+M[2], M[0]-1, +M[1]).getDay()]+ 'day';
        }
        return null;
    }
    var input={value:'04/26/2013 2:51:00 PM '}; // fake input field
    dowFromDmy(input);

    /* returned value: (String)
    Friday
    */
    Pretty Slick! (Other than your spelling of Saturday )

    I added some test inputs.
    1. Handles different styles of input very well.
    2. Also, converts inputs a best it can, ie: 4/0/2013 and 4/32/2013.
    3. Defaults to blank (null) with invalid entries (PM only).

    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    function dowFromDmy(info){
        var M= info.match(/(\d+)/g);
        if(M){
            return 'Sun,Mon,Tues,Wednes,Thurs,Fri,Satur'
                   .split(',')[new Date(+M[2], M[0]-1, +M[1]).getDay()]+ 'day';
        } // else { alert('Invalid input'); }  // for testing purposes
        return null;
    }
    </script>
    
    </head>
    <body>
    DOW caluclator test: <select onchange="SHOWdowFromDmy(this.value)">
     <option value="">Sample inputs</option>
     <option value="04/26/2013 2:51:00 PM"> 04/26/2013 2:51:00 PM (Friday)</option>
     <option value="4/1/2013"> 4/1/2013 (Monday)</option>
     <option value="4/0/2013"> 4/0/2013 (Sunday)</option>
     <option value="4/32/2013"> 4/32/2013 (Thursday)</option>
     <option value=""> Empty input () </option>
     <option value=""> -- Other Inputs -- </option>
     <option value="PM"> PM (Invalid--&gt;null) </option>
     <option value="4:1:2013 2:51:00 PM"> 4:1:2013 PM (Monday)</option>
     <option value="4-1-2013"> 4-1-2013 2:51:00 PM (Monday)</option>
    </select>
    <div id="dowDIV"></div>
    
    <script type="text/javascript">
    function SHOWdowFromDmy(info) {
      document.getElementById('dowDIV').innerHTML=dowFromDmy(info);
    }
    </script>
    
    </body>
    </html>
    Last edited by JMRKER; 04-24-2013 at 12:40 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles