Date converter

Hi all,
any help at all appreciated.
Im trying to convert this date
13112006 [ddmmyyyy]
into a variable where i can get the day name from it.
my code so far…


            date1 = "13112006";
            date2 = "10102006";
            date3 = "10102006";
            date4 = "10102006";

            d1 = Date(date2);
            d2 = Date(date2);
            d3 = Date(date3);
            d4 = Date(date4);
            
weekday =  new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

            date1 = weekday[d1.getDay()];
            date2 = weekday[d2.getDay()];
            date3 =weekday[d3.getDay()];
            date4 = weekday[d4.getDay()];
            trace(date1 + date2 + date3 + date4)

any suggestions?

Well 13112006 is not goin to be a recognised date value for flash. The easiest way would be to just split that variable into its day, month and year, then put that into a Date class.

ok - so for instance
i have this

date2="18102006";//ddmmyyyy

var my_str2:String = new String(date2);

var mySubstring2:String = new String();
mySubstring2_1 = my_str2.substr(0,2);//day
mySubstring2_2 = my_str2.substr(2,2);//month
mySubstring2_3 = my_str2.substr(4,4);//year

day2 =mySubstring2_3+ mySubstring2_1+mySubstring2_2;

myDate2 = new Date(day2);

weekday =  new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

            date2new = weekday[myDate2.getDay()];

trace(date2new)//this comes up undefined

it seem the vars are being put into the right place by the new Date bit is not converting it into a new date.

any ideas?

With Publish settings of Flash Player 8, Actionscript 2 it works fine - are you sure you’re not using Actionscript 1 settings?

If you’d prefer to use AS 1, just delete the :String data types and it should also work.

Hi,
no im definately using flash 8 actionscript 2.
Just by using this code on its own, ie a new movie, the traced day comes out as “thursday” no matter what ddmmyyy i set at the start of the code.

What day do you see?

[quote=glosrfc;1984770]With Publish settings of Flash Player 8, Actionscript 2 it works fine - are you sure you’re not using Actionscript 1 settings?

If you’d prefer to use AS 1, just delete the :String data types and it should also work.[/quote]

Ahhh, I got Thursday too - I thought you said it was coming up as undefined.

And the reason you’re getting Thursday is because date2 = 20061810. When you convert this into a date using new date(date2) it assumes that value is milliseconds which it adds on to midnight Jan 1 1970. Which happens to have been a Thursday.

Here’s how I’d go about achieving the same exercise:

date2="18102006";//ddmmyyyy
 
// extract portions of date from date2 string
var my_str2:String = new String(date2);
var mySubstring2:String = new String();
mySubstring2_1 = my_str2.substr(0,2);//day
mySubstring2_2 = my_str2.substr(2,2);//month
mySubstring2_3 = my_str2.substr(4,4);//year
 
// convert strings into numbers
day = Number(mySubstring2_1);
month = Number(mySubstring2_2) - 1; // Takes account of Jan = 0, Feb = 1, etc.
year = Number(mySubstring2_3);
 
// construct date and change this to the numeric values obtained from date2
var myDate2:Date = new Date();
myDate2.setFullYear(year, month, day);
 
// calculate weekday
weekday =  new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
            date2new = weekday[myDate2.getDay()];
trace(date2new)// should be Wednesday

Hi - yeah it was coming up undefined when in the rest of the movie (must be something else conflicting)

hey - thanks so much. This works perfectly. Ive spent days on google and trial and error so this is a major relief for what i though shouldve been an easy prob for me to fix.
Much appreciated!

[quote=glosrfc;1985027]Ahhh, I got Thursday too - I thought you said it was coming up as undefined.

And the reason you’re getting Thursday is because date2 = 20061810. When you convert this into a date using new date(date2) it assumes that value is milliseconds which it adds on to midnight Jan 1 1970. Which happens to have been a Thursday.

Here’s how I’d go about achieving the same exercise:
ActionScript Code:
[FONT=Courier New][LEFT]date2=[COLOR=#FF0000]“18102006”[/COLOR];[COLOR=#808080]//ddmmyyyy[/COLOR]

[COLOR=#808080]// extract portions of date from date2 string[/COLOR]
[COLOR=#000000]var[/COLOR] my_str2:[COLOR=#0000FF]String[/COLOR] = [COLOR=#000000]new[/COLOR] [COLOR=#0000FF]String[/COLOR]COLOR=#000000[/COLOR];
[COLOR=#000000]var[/COLOR] mySubstring2:[COLOR=#0000FF]String[/COLOR] = [COLOR=#000000]new[/COLOR] [COLOR=#0000FF]String[/COLOR]COLOR=#000000[/COLOR];
mySubstring2_[COLOR=#000080]1[/COLOR] = my_str2.[COLOR=#0000FF]substr[/COLOR]COLOR=#000000[/COLOR];[COLOR=#808080]//day[/COLOR]
mySubstring2_[COLOR=#000080]2[/COLOR] = my_str2.[COLOR=#0000FF]substr[/COLOR]COLOR=#000000[/COLOR];[COLOR=#808080]//month[/COLOR]
mySubstring2_[COLOR=#000080]3[/COLOR] = my_str2.[COLOR=#0000FF]substr[/COLOR]COLOR=#000000[/COLOR];[COLOR=#808080]//year[/COLOR]

[COLOR=#808080]// convert strings into numbers[/COLOR]
day = [COLOR=#0000FF]Number[/COLOR]COLOR=#000000[/COLOR];
month = [COLOR=#0000FF]Number[/COLOR]COLOR=#000000[/COLOR] - [COLOR=#000080]1[/COLOR]; [COLOR=#808080]// Takes account of Jan = 0, Feb = 1, etc.[/COLOR]
year = [COLOR=#0000FF]Number[/COLOR]COLOR=#000000[/COLOR];

[COLOR=#808080]// construct date and change this to the numeric values obtained from date2[/COLOR]
[COLOR=#000000]var[/COLOR] myDate2:[COLOR=#0000FF]Date[/COLOR] = [COLOR=#000000]new[/COLOR] [COLOR=#0000FF]Date[/COLOR]COLOR=#000000[/COLOR];
myDate2.[COLOR=#0000FF]setFullYear[/COLOR][COLOR=#000000]([/COLOR]year, month, day[COLOR=#000000])[/COLOR];

[COLOR=#808080]// calculate weekday[/COLOR]
weekday = [COLOR=#000000]new[/COLOR] [COLOR=#0000FF]Array[/COLOR][COLOR=#000000]([/COLOR][COLOR=#FF0000]“Sunday”[/COLOR], [COLOR=#FF0000]“Monday”[/COLOR], [COLOR=#FF0000]“Tuesday”[/COLOR], [COLOR=#FF0000]“Wednesday”[/COLOR], [COLOR=#FF0000]“Thursday”[/COLOR], [COLOR=#FF0000]“Friday”[/COLOR], [COLOR=#FF0000]“Saturday”[/COLOR][COLOR=#000000])[/COLOR];
date2new = weekday[COLOR=#000000][[/COLOR]myDate2.[COLOR=#0000FF]getDay[/COLOR]COLOR=#000000[/COLOR][COLOR=#000000]][/COLOR];
[COLOR=#0000FF]trace[/COLOR]COLOR=#000000[/COLOR][COLOR=#808080]*// should be Wednesday *[/COLOR]

[/LEFT]
[/FONT]

[/quote]

No problem…always happy to help