Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •