logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > Javascript and JS frameworks
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 03-01-2014, 08:03 PM   #1
katy-rome
 
Join Date: Mar 2014
Posts: 2
Default javascript for sequential day numbers

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!
katy-rome is offline   Reply With Quote
Old 03-20-2014, 03:46 AM   #2
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

Code:
<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
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 03-20-2014, 03:54 AM   #3
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

this should exclude the weekend
Quote:
<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>
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 03-22-2014, 07:29 AM   #4
katy-rome
 
Join Date: Mar 2014
Posts: 2
Default Thank you!

This is fantastic, really appreciated. Works perfectly. Thank you very much for taking the time to help me.
katy-rome is offline   Reply With Quote
Old 03-24-2014, 02:11 AM   #5
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

welcome, actually justa follow on from anotherpost so no bigge
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 04-02-2014, 07:59 PM   #6
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

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,
Code:
<!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]
__________________
------------
Jim Michaels
HTML Code:
improperly<strong>nested<em>elements</strong>cause</em>
browser confusion (I believe the term is 'tag soup')!
jmichae3 is offline   Reply With Quote
Old 04-02-2014, 11:32 PM   #7
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

working code:
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>
__________________
------------
Jim Michaels
HTML Code:
improperly<strong>nested<em>elements</strong>cause</em>
browser confusion (I believe the term is 'tag soup')!
jmichae3 is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 12:11 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com