Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Date Generation...can't quite picture this

Options
  • 09-08-2004 10:27pm
    #1
    Registered Users Posts: 68,317 ✭✭✭✭


    Right-ho, this is probably something easy, but I can't quite seem to get it.

    I'm writing some scripts for a helpdesk backend that allows users to generate weekly performance metrics. The SQL and most of the coding is no issue, it's a simple matter of display.
    Basically, they choose a week from a drop-down, hit "generate", and a script queries a database, and blah blah blah.

    It's generating the dates that's getting to me :)

    I need to generate a list of weeks, e.g. "Monday 02/08/2004 - Friday 06/08/2004", and I've no idea how to generate this list. Even if I could get a list of Mondays (02/08/2004, 09/08/2004, ....), that would be enough. I don't want to just pick a random Monday a year ago or whatever, and then add 7 on to each successive item to get the next Monday - Leap years would screw everything up.

    I'm doing it in Coldfusion, but give me plain theory or code in any other language, and I can come up with it.

    Cheers


Comments

  • Closed Accounts Posts: 35 Ivan Dunaev


    leap years doesn't make weeks longer, so adding 7 days will work anyway


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    leap years doesn't make weeks longer, so adding 7 days will work anyway
    Yeah, I thought about that alright. I think it's just not sitting right in my head. I'll tinker about and see what I can come up with...


  • Registered Users Posts: 9 YellowMan


    Generate an any number between 1 and 51.
    If a year is important, use something like this :

    In SQL

    select dateadd(wk,@myRandom,'20040101'),dateadd(wk,@myRandom+1,'20040101')

    In VB
    print dateadd("wk",myRandom,"20040101"),dateadd("wk",myRandom+1,"20040101")


  • Registered Users Posts: 15,989 ✭✭✭✭blorg


    Hi Seamus,

    Sure you have the answer by now, but the DateAdd() function can easily add a week to a date and will handle leap years etc.

    E.g.

    <cfscript>
    TheYear="2000";
    </cfscript>

    <!--- Find first Monday of the year --->
    <cfloop from="1" to="7" index="i">
    <cfscript>
    if (DateFormat("#TheYear#-01-#i#","ddd") eq "Mon") {
    FirstMon = CreateDate(TheYear, 1, i);
    break;
    }
    </cfscript>
    </cfloop>

    <cfloop from="0" to="52" index="i">
    <cfscript>
    SoW = DateAdd("ww", i, FirstMon);
    </cfscript>
    <cfoutput>
    #DateFormat(Sow,"ddd dd/mm/yyyy")#<br>
    </cfoutput>
    </cfloop>


Advertisement