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

• 04-22-2013, 03:04 PM
jaykappy
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
• 04-22-2013, 03:56 PM
JMRKER
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>```
:)
• 04-22-2013, 06:14 PM
007Julien
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...)
• 04-22-2013, 08:21 PM
JMRKER
Wow! Where in the world did you find that formula???
• 04-23-2013, 04:16 AM
007Julien
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 !
• 04-23-2013, 11:58 PM
mrhoo
// 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
*/
• 04-24-2013, 08:33 AM
wbport
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.
• 04-24-2013, 09:50 AM
JMRKER
Quote:

Originally Posted by mrhoo
// 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 :) )

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>```

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.11584 seconds
• Memory Usage 2,383KB
• Queries Executed 11 (?)
Template Usage (21):
• (6)bbcode_code_printable
• (1)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates