PDA

View Full Version : javascript for sequential day numbers


katy-rome
03-01-2014, 08:03 PM
Can somebody help me with javascript to insert into an html page which will display Day 1, Day 2 etc sequentially up to Day 6, but would exclude weekend days?
Or else, a script that would just do the day numbers and that I could manually adjust over the weekend.. would that work?
Any help appreciated!

edbr
03-20-2014, 03:46 AM
<html>
<head>
<title>JavaScript getDay Method</title>
</head>
<body>
<script type="text/javascript">
var dt = new Date();
document.write( "Day " + dt.getDay() );
</script>
</body>
</html>
will give the day number

edbr
03-20-2014, 03:54 AM
this should exclude the weekend
<html>
<head>
<title>JavaScript getDay Method</title>
</head>
<body>
<script type="text/javascript">
var dt = new Date();
var d = dt.getDay();
if(d>=0 & d<= 6) {
document.write( "Day " + d );
}
</script>
</body>
</html>

katy-rome
03-22-2014, 07:29 AM
This is fantastic, really appreciated. Works perfectly. Thank you very much for taking the time to help me.

edbr
03-24-2014, 02:11 AM
welcome, actually justa follow on from anotherpost so no bigge

jmichae3
04-02-2014, 07:59 PM
ahh, I got this. javascript specification states that there's a Date.getDay() which returns a 0-based day of week where Sunday is 0.
so,

<!DOCTYPE html>
<html>
<head>
<title>dow without weekends</title>
</head>
<body>
<script>
document.,write
dowNoWeekends("");
dowNoWeekends("2/12/2014");
function normalDateToGMT(curdate) {//assume M/D/YYYY
//convert day

var months=",January,February,March,April,May,June,July,August ,September,October,November,December".split(',');//make 0-based array
var daysofweek="Sun Mon Tue Wed Thu Fri Sat".split(' ');//make 0-based array
curdate=curdate.split('/');
for (i=0; i < curdate.length; i++) {
curdate[i]=parseInt(curdate[i]);
}
var d=new Date(curdate[2], curdate[0], curdate[1]);
return d.toUTCSTring();
function dowNoWeekends(curdate) {
var s="",i;
var months=",January,February,March,April,May,June,July,August ,September,October,November,December".split(',');//make 0-based array
var daysofweek="Sun Mon Tue Wed Thu Fri Sat".split(' ');//make 0-based array
var today.new Date(normalDateToGMT(curdate));
var dow=today.getDate();//day of week
if (dow <= 0 || dow >= 6) {
dow=1; //monday
}
curDate.getDay();
</script>
</body>
</html>


ahh skip it, something simpler. your specification is very vague. for instance, are these workweeks?
do you want anything at all displayed on the weekends other than the day name? because that's easy.
does day 1 need to start in the middle of the week? that would make things a LOT more complicated.
[code]
<script>
showweeks(5);
function showweeks(nweeks) {
//do header
var daysofweek="Sun Mon Tue Wed Thu Fri Sat".split(' ');//make 0-based array
var s="<table><tr>";
for i=0; i< daysofweek.length; i++) {
s+="<th>"+daysofweek[i]+"</th>";
}
s+="</tr>";
//do body
var weeklength=7-2;
for(var daynum=1; daynum < nweeks*weeklength; daynum++) {

if (0==(daynum-1)%weeklength) { //handle Monday
s+="<tr><td>&nbsp</td><td>"+daynum+"</td>";
} else if (0==(daynum-1)%weeklength) {//handle Friday
s+="<td>"+daynum+"</td><td>&nbsp</td><tr>\n";
} else {
s+="<td>"+daynum+"</td>;
}
}
s+="</table>";
document.getElementById(id).innerHTML=s;
</script>
[code]

jmichae3
04-02-2014, 11:32 PM
working code:

<!DOCTYPE html>
<html>
<head>
<title>dow without weekends</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<style>
caption{
border-style:solid;
border-width:thin;
background-color:#cff;
}
</style>
<div id="o"></div>
<script>
var workweek=7-2,Sun=0,Mon=1,Tue=2,Wed=3,Thu=4,Fri=5,Sat=6;
showWeeks("o", 2*workweek, Thu,"WorkWeeks");
function showWeeks(id, numdays, dowToStartOn,captionContent) {
//sunday=0, monday=1, friday=5, saturday=6
//do header
var daysofweek="Sun Mon Tue Wed Thu Fri Sat".split(' ');//make 0-based array
var s="<table border='1'><caption>"+captionContent+"</caption><tr>";
var dowCounter=Sun;
for (dowCounter=Sun; dowCounter< daysofweek.length; dowCounter++) {
s+="<th>"+daysofweek[dowCounter]+"</th>";
}
s+="</tr>";
dowCounter=0;//reset dow
//do body
//make corrections to request for start of day of week
if (dowToStartOn<1|| dowToStartOn > 5) {
dowToStartOn=1;//monday
}
var firstWeek=true,daynum=1;
var weeklength=7-2;

//pre-numbered days should be blank
dowCounter=0;
while (dowCounter<dowToStartOn) { //dowToStartOn will always be < Sat (6)
switch(dowCounter) {
case Sun:
s+="<tr><td>&nbsp;</td>";
break;
case Sat:
s+="<td>&nbsp;</td></tr>";
break;
default:
s+="<td>&nbsp;</td>";
break;
}
dowCounter=(dowCounter+1)%7;//increment day of week within a week's period
}
//numbered days should have content in them
while (daynum <= numdays) {
switch(dowCounter) {
case Sun:
s+="<tr><td>&nbsp;</td>";
break;
case Sat:
s+="<td>&nbsp;</td></tr>";
break;
default: //days 1-5 or mon-fri
s+="<td>"+daynum+"</td>";
daynum++;
break;
}
dowCounter=(dowCounter+1)%7;//increment day of week within a week's period
}
while (dowCounter < 7) {
switch(dowCounter) {
case Sun:
s+="<tr><td>&nbsp;</td>";
break;
case Sat:
s+="<td>&nbsp;</td></tr>";
break;
default:
s+="<td>&nbsp;</td>";
break;
}
dowCounter++;
//increment day of weekcounter, but we want to
//make this stop at 6 instead of just being a forever loop,
//so will not use the usual increment math.
}
s+="</table>";
document.getElementById(id).innerHTML=s;
}
</script>
</body>
</html>