...

View Full Version : Resolved get array of days based on year and weeknr



vsempoux01
08-08-2008, 09:00 AM
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.

mcjwb
08-08-2008, 04:10 PM
Hi,

Inspired by the functions on this page (http://www.digital-web.com/articles/javascript_date_object_with_user_methods/), this is what I've come up with:


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

vsempoux01
08-11-2008, 11:02 AM
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.

mcjwb
08-11-2008, 12:59 PM
Unfortunately JavaScript doesn't have a date format function so we have to create our own:

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:

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

vsempoux
08-22-2008, 09:29 AM
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.

mcjwb
08-22-2008, 10:38 AM
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):

var startDate = new Date().dateFirstMonday(Year);
If this doesn't help, you could post your code and I could take a look at it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum