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

1. Registered User
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. One of many ways:
Code:
```<!DOCTYPE HTML>
<html>
<title> Untitled </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<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. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
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;});
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 06:47 PM.

4. Wow! Where in the world did you find that formula???

5. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
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 04:19 AM.

6. Registered User+
Join Date
Feb 2006
Posts
2,930
// 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. 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. 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 )

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

<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 11:40 AM.

##### 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
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.16610 seconds
• Memory Usage 2,929KB
• Queries Executed 15 (?)
Template Usage (34):
• (6)bbcode_code
• (1)bbcode_quote
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (8)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (8)postbit
• (8)postbit_onlinestatus
• (8)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./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/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (72):
• init_startup
• friendlyurl_resolve_class
• 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_postinfo_query
• fetch_postinfo
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete