Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Date Generation...can't quite picture this

  • 09-08-2004 10:27PM
    #1
    Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭


    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, Registered Users 2 Posts: 68,173 ✭✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 15,995 ✭✭✭✭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