Search code examples
actionscript-3datetimedate-formatting

find tomorrow day, and the day after tomorrow in AS3


I've got this code :

    var currentDate=new Date();
var my_date:Date = new Date();
var month=currentDate.getMonth()+1;
var day=currentDate.getDate();
var day2=currentDate.getDate()+1;
var day3=currentDate.getDate()+2;
var day4=currentDate.getDate()+3;
var day5=currentDate.getDate()+4;
var year=currentDate.getFullYear();
var tomorrow;
var three;
var four;
var five;
var months:Array = ["janvier", "fevrier", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "decembre"];
var today =(day+" "+ months[my_date.month]);
var days:Array = ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"];

I'd like to have the day of tomorrow (and the 5 next ones).

I've tried :

tomorrow =(days[my_date.day+1]+" "+day2 +" "+ months[my_date.month]+" "+ year);
    trace(tomorrow);

It's working. I've got "samedi 5 juillet 2015"

but then I've tried :

 day3 =(days[my_date.day+2]+" "+day2 +" "+ months[my_date.month]+" "+ year);
trace(day3 );

But it results with "undefined 6 juillet 2015".

Do you know how I could do it ?

Thank you


EDIT

So I've add to my code for the name days:

var index:int = my_date.day + 2
if(index >= days.length)
{
    index -= days.length;
}

So here's my code :

var currentDate=new Date();
var my_date:Date = new Date();
var month=currentDate.getMonth()+1;
var day=currentDate.getDate();
var day2=currentDate.getDate()+1;
var day3=currentDate.getDate()+2;
var year=currentDate.getFullYear();
var tomorrow;
var three;
var four;
var five;
var six;
var months:Array = ["janvier", "fevrier", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "decembre"];
var days:Array = ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"];

var today =(days[my_date.day]+" "+day +" "+ months[my_date.month]+" "+ year);
var index:int = my_date.day + 2
if(index >= days.length)
{
    index -= days.length;
}
trace(days[index+6]);

I've add these lines for sunday has the were a bug (saturday was "undefined") :

if(days[index] == "dimanche"){
 tomorrow =(days[index+6]+" "+day2 +" "+ months[my_date.month]+" "+ year);
}else{
 tomorrow =(days[index-1]+" "+day2 +" "+ months[my_date.month]+" "+ year);
}

And then

three = (days[index] + " " + day3 + " " + months[my_date.month] + " " +  year);
    four = (days[index+1] + " " + day4 + " " + months[my_date.month] + " " +  year);
    five = (days[index+2] + " " + day3 + " " + months[my_date.month] + " " +  year);
}

But it seems that it doesn't work with all days.

For the "third july', it's working great :

enter image description here

But if I change the computer date and choose, for exemple, the 23d of July

enter image description here

Some days are "undefined".

Any idea why ?


Solution

  • To show future dates using months and days arrays, you can do like this :

    var months:Array = ['janvier', 'fevrier', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'decembre'],
        days:Array = ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi'];
    
    function show_7_days(date:Date): void {
        var future_date:Date = new Date(date.fullYear, date.month, date.date);
        for(var i:int = 1; i < 8; i++){
            future_date.date = date.date + i;       
            trace(
                days[future_date.getDay()] + ' ' 
                + future_date.getDate() + ' ' 
                + months[future_date.getMonth()] + ' ' 
                + future_date.getFullYear()
            );
        }
    }
    

    And then :

    show_7_days(new Date());
    
    // gives : 
    //      samedi 4 juillet 2015
    //      dimanche 5 juillet 2015
    //      lundi 6 juillet 2015
    //      mardi 7 juillet 2015
    //      mercredi 8 juillet 2015
    //      jeudi 9 juillet 2015
    //      vendredi 10 juillet 2015
    

    And for a specific date :

    show_7_days(new Date(1900, 3, 3));
    
    // gives : 
    //      mercredi 4 avril 1900
    //      jeudi 5 avril 1900
    //      vendredi 6 avril 1900
    //      samedi 7 avril 1900
    //      dimanche 8 avril 1900
    //      lundi 9 avril 1900
    //      mardi 10 avril 1900
    

    But you can also simplify things using a flash.globalization.DateTimeFormatter like this :

    var date_formater:DateTimeFormatter = new DateTimeFormatter('fr-FR', DateTimeStyle.LONG, DateTimeStyle.NONE);
    
    function show_7_days(date:Date): void {
        var future_date:Date = new Date(date.fullYear, date.month, date.date);
        for(var i:int = 1; i < 8; i++){
            future_date.date = date.date + i;       
            trace(date_formater.format(future_date));
        }
    }
    

    Which will give you the same results.

    Hope that can help.