dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: date

  1. #1
    Member
    Join Date
    Oct 2000
    Posts
    78
    i have a current news section within a site that uses a script to retrieve the current date. it takes the date and loads a .txt with the corresponding name.

    i.e. 10/5/01 would retrieve 10501.txt

    all works fine. i would also like to have a small archive for loading news for the previous 6 days. i could obviously do all this manually, but dynamic is what i am trying to work towards.

    my question is, is it possible for my archive to ONLY load the previous 6 dates dynamically.

    i.e. today is 10/5/01

    my archive would contain 6 buttons that load:

    10401.txt
    10301.txt
    10201.txt
    10101.txt
    93001.txt
    92901.txt

    so basically it would look at today's date, and subtract 6 days without coming up with something like:

    10/-1/01

    all my sad attempts have failed pathetically, so if anyone could shed some light i would appreciate it.

  2. #2
    Senior Member
    Join Date
    Jan 2001
    Posts
    472
    If your using a Date object, then getDay() will return the specific day that you would then subtract from. If the difference is less than one, you'd subtract a month and find the correct number of days for that month and count back from that. I put this together quickly to give you some ideas. It doesn't take into account leap years, but it does work to find the current day and traces the previous 6 days:

    function Buffer(num) {
    num = num.toString();
    if (num.length < 2) {
    num = "0" + num;
    }
    return num;
    }
    daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    todayDate = new Date();
    today = Buffer(todayDate.getDay());
    thisMonth = Buffer(todayDate.getMonth() + 1);
    thisYear = String(todayDate.getYear()).substr(1);
    for (j = 1; j < 7; j++) {
    thisDay = today - j;
    tempMonth = thisMonth;
    tempYear = thisYear;
    if (thisDay < 1) {
    tempMonth -= 1;
    if (tempMonth < 0) {
    tempMonth = 11;
    tempYear -= 1;
    }
    thisDay = daysInMonth[tempMonth - 1] + thisDay;
    tempMonth = Buffer(tempMonth)
    }
    thisDay = Buffer(thisDay);
    trace(""+tempMonth+thisDay+tempYear)
    }


    Buffer is a function that ensures the date is at least two digits. The array stores the number of days in each month in case you have to go back a month. This what you need?


    --tyard
    http://www.27Bobs.com

  3. #3
    Senior Member
    Join Date
    Jan 2001
    Posts
    472
    Rereading your post, I see you don't need the Buffer, but otherwise the code should give you the dates you need.

  4. #4
    Senior Member
    Join Date
    Oct 2000
    Posts
    519
    Guys there is a much easier way! If you have the current date in a date object you can just subtract from it. This is because the date object works by storing the date as a number of milliseconds from a specific date in the past (jan 1st 1970 I think). All you have to do is convert the amount you want to subtract into milliseconds and the date object will take care of everything else for you like leap years months etc.

    Code:
    var now = new Date();
    
    //days * hours * minutes * seconds * milliseconds
    sixDays = 6 * 24 * 60 * 60 * 1000;
    
    var then = now - sixDays;
    trace(now.toString());
    trace(then.toString());
    This will give you a new date objects which you can get the date out of as normal.

  5. #5
    Senior Member
    Join Date
    Jan 2001
    Posts
    472
    Much easier. Thanks.


    --tyard
    http://www.27Bobs.com

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center