Search code examples
actionscript-3dateperiod

Get date from period number in Actionscript


I have a table which containt a date, a number for the number of weeks per period, and a year. the user then enters a date and I can calculate the period number from this. But I'd like to do it the other way too: Entering a period number and get the start and end date of the period from this. Unfortunately, I can't seem to get the right logic. Could anyone guide me with this?

Thank you.

EDIT: options[0] being the start date from the database and options[1] the number of weeks for one period.

This is the function I already have and which works:

private function dateToPeriod(date:Date):Number
    {
        var d = new Date(options[0]);
        var periode = Math.floor((date.time - d.time)/(604800000*options[1])+1);
        return periode;
    }

let's say my start date it 12/12/2009, then passing 12/12/2009 to this function would return 1 since it's the first "period" from this date (in week number).

What I want is to make a periodToDate function.


Solution

  • EDITED ANSWER BASED ON NEW INFO

    Alright, this is pretty simple then. You can add values onto the date property. Try this, again, not tested.

    public function addPeriodToDate(date:Date, period:int, numWeeksInPeriod:int):Date
    {
        var periodDate:Date = new Date(date.time);
        periodDate.date += period * numWeeksInPeriod;
        return periodDate;
    }
    

    END EDIT

    I haven't tested this, just some quick code, but I think this should get you going in the right direction.

    private function dateToPeriod(date:Date):Number
    {
        var d = new Date(options[0]);
        var diffInMilliseconds:Number = date.time - d.time;
        var diffInWeeks:Number = diffInMilliseconds / 1000 / 60 / 60 / 24 / 7;
        var weeksInPeriod:Number = options[1];
        var period:int = diffInWeeks / weeksInPeriod + 1;
    
        return period;
    }