Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-08-2008, 08:00 AM   PM User | #1
vsempoux01
New to the CF scene

 
Join Date: Aug 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
vsempoux01 is an unknown quantity at this point
get array of days based on year and weeknr

Hello,

on my form i have two selectionlists, one with year selection and another
with weeknumber selection.
Next i have a table like this
day date val
-----------------------------------
monday xxx 1 day worked
tuesday xxx 0.5 day worked
wednesday xxx 0 day worked
thursday
friday
saturday
sunday
------------------------------------
total for week x 2.5 day(s) worked

is there a javascript function that can return me an array with the dates from a chosen
week for a chosen year? (eventualy date formated dd-MM-yyyy)
I have already the ValY(year value) and ValW(weeknumber value).
the array returned should be like [04-08-2008,05-08-2008,06-08-2008,07-08-2008,
08-08-2008,09-08-2008,10-08-2008] where 04-08-2008 is monday and 10-08-2008 sunday.

If anybody could give me some pointers or a link on the web how i can create this function.
Many thanks in advance.

V.

Last edited by WA; 08-12-2008 at 03:19 AM..
vsempoux01 is offline   Reply With Quote
Old 08-08-2008, 03:10 PM   PM User | #2
mcjwb
Regular Coder

 
Join Date: Jul 2007
Location: UK
Posts: 223
Thanks: 0
Thanked 14 Times in 14 Posts
mcjwb is an unknown quantity at this point
Hi,

Inspired by the functions on this page, this is what I've come up with:

Code:
<html>
<head>
<script type="text/javascript">
/*
return of Date().getDay()
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
*/
Date.prototype.dateFirstMonday = function(Year){
    if(!Year){
        var now = new Date();
        Year = now.getFullYear();
    }
    var Firstday = new Date("January 1, "+Year+" 00:00:00");

    var DayOfWeek = parseInt(Firstday.getDay());

    if(DayOfWeek >= 2 && DayOfWeek <=4){
        Firstday.addDays((1-DayOfWeek));
    }
    else if(DayOfWeek >= 5){
        Firstday.addDays(7-(DayOfWeek-1));
    }
    else if(DayOfWeek == 0){
        Firstday.addDays(1);
    }

    return Firstday;
}

Date.prototype.copy = function () {
  return new Date(this.getTime());
};

Date.prototype.addDays = function(d) {
  this.setDate( this.getDate() + d );
};

Date.prototype.addWeeks = function(w) {
  this.addDays(w * 7);
};

function getWeekDates(Year, Week){
    var startDate = new Date().dateFirstMonday(Year);

    //To get the start date of the given week we need to add "Week-1" weeks otherwise we'd get the first day of the following week.
    startDate.addWeeks(Week-1);

    var weekDates = Array(7);
    for (var i=0; i<=6; i++){
        var thisDate = startDate.copy();
        weekDates[i] = thisDate;

        startDate.addDays(1);
    }
    return weekDates;
}

var dates = getWeekDates(2008, 32);

var msg='';
for(var i=0; i<dates.length; i++){
    msg+=dates[i].toString() + "\r\n";
}
alert(msg);
</script>
</head>
<body>
</body>
</html>
The function getWeekDates() returns an array of Date objects, hopefully this is what you're after. If you want any of it explaining don't hesitate to ask.
__________________
Javascript Debugging Tools:
IE Script Debugger | IE Developer Toolbar | Fiddler || FF Firebug | FF Web Developer
mcjwb is offline   Reply With Quote
Old 08-11-2008, 10:02 AM   PM User | #3
vsempoux01
New to the CF scene

 
Join Date: Aug 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
vsempoux01 is an unknown quantity at this point
Hello, thanks for your response.
The script is working great, but i was just wondering how i can get my dates in the
format dd/MM/yyyy?

I know it must be something stupid. I tried already to apply the format option but it gave me some errors.

Again, thanks for the reply.
V.
vsempoux01 is offline   Reply With Quote
Old 08-11-2008, 11:59 AM   PM User | #4
mcjwb
Regular Coder

 
Join Date: Jul 2007
Location: UK
Posts: 223
Thanks: 0
Thanked 14 Times in 14 Posts
mcjwb is an unknown quantity at this point
Unfortunately JavaScript doesn't have a date format function so we have to create our own:
Code:
Date.prototype.ukFormat = function(){
 var days = this.getDate();
 if (days<10) { days = '0'+days; }
 var months = this.getMonth() + 1;//Add one since getMonth starts at 0 for January.
 if (months<10) { months = '0'+months; }
 var years = this.getFullYear();
 return days + '/' + months + '/' + years;
}
This is a very simple one that is not flexible in the slightest, but will give you the date in the format dd/mm/yyyy.

Adding this to the script I originally posted you get:
Code:
<html>
<head>
<script type="text/javascript">
/*
return of Date().getDay()
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
*/
Date.prototype.dateFirstMonday = function(Year){
    if(!Year){
        var now = new Date();
        Year = now.getFullYear();
    }
    var Firstday = new Date("January 1, "+Year+" 00:00:00");
    var DayOfWeek = parseInt(Firstday.getDay());
    if(DayOfWeek >= 2 && DayOfWeek <=4){
        Firstday.addDays((1-DayOfWeek));
    }
    else if(DayOfWeek >= 5){
        Firstday.addDays(7-(DayOfWeek-1));
    }
    else if(DayOfWeek == 0){
        Firstday.addDays(1);
    }
    return Firstday;
}
Date.prototype.copy = function () {
  return new Date(this.getTime());
};
Date.prototype.addDays = function(d) {
  this.setDate( this.getDate() + d );
};
Date.prototype.addWeeks = function(w) {
  this.addDays(w * 7);
};
Date.prototype.ukFormat = function(){
 var days = this.getDate();
 if (days<10) { days = '0'+days; }
 var months = this.getMonth() + 1;//Add one since getMonth starts at 0 for January.
 if (months<10) { months = '0'+months; }
 var years = this.getFullYear();
 return days + '/' + months + '/' + years;
}
function getWeekDates(Year, Week){
    var startDate = new Date().dateFirstMonday(Year);
    //To get the start date of the given week we need to add "Week-1" weeks otherwise we'd get the first day of the following week.
    startDate.addWeeks(Week-1);
    var weekDates = Array(7);
    for (var i=0; i<=6; i++){
        var thisDate = startDate.copy();
        weekDates[i] = thisDate;
        startDate.addDays(1);
    }
    return weekDates;
}
var dates = getWeekDates(2008, 32);
var msg='';
for(var i=0; i<dates.length; i++){
    msg+=dates[i].ukFormat() + "\r\n";
}
alert(msg);
</script>
</head>
<body>
</body>
</html>
Hope that helps.
mcjwb is offline   Reply With Quote
Old 08-22-2008, 08:29 AM   PM User | #5
vsempoux
New to the CF scene

 
Join Date: Aug 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
vsempoux is an unknown quantity at this point
Hello again,

Your last code worked excellent in a plain html page.

When i implemented this code into my application it doesn't work!
After some searching it found out that i cannot use extra classes in my functions (all the prototype functions) ..
I've been trying from quite some time now to convert your code into
separate functions, but somehow it doesn't quite work.

If you mind, could you transform your code into separate functions, this would be a great help for me.

Thanks alot.
vsempoux is offline   Reply With Quote
Old 08-22-2008, 09:38 AM   PM User | #6
mcjwb
Regular Coder

 
Join Date: Jul 2007
Location: UK
Posts: 223
Thanks: 0
Thanked 14 Times in 14 Posts
mcjwb is an unknown quantity at this point
I'm not aware of any reason why the prototype methods wouldn't work for you, "prototype" is built into JavaScript. One thing to note is that when extending built in objects (like Date) with the prototype method, then the extended methods will only be available if the (Date) object was instantiated with the "new" keyword.
For example (from the code I posted):
Code:
var startDate = new Date().dateFirstMonday(Year);
If this doesn't help, you could post your code and I could take a look at it.
__________________
Javascript Debugging Tools:
IE Script Debugger | IE Developer Toolbar | Fiddler || FF Firebug | FF Web Developer
mcjwb is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 +1. The time now is 04:37 AM.


Advertisement
Log in to turn off these ads.