PDA

View Full Version : Counting through an array?


Laura_K
11-11-2008, 10:13 AM
Hello,

I'm trying to create a calendar script and have become stuck with finding out which piece of code I need. I'm not a total beginner so I thought I would post my question here.

Basically I have two dynamic text boxes which return the current day of the week as a number and another one with the current date within the month. This I have created using the get date function which reads from your computer's date. So for today one reads 2 for Tuesday and 11 for the current date.

What I am now trying to do is work out what day the first of the month falls on. Now I know I can do this by counting back the current date amount through an array that contains text to represent which day of the week it is. But I'm having trouble working out how to do this and I really need to work this out asap.

I apologize if my explanation is not very clear, but any help or ways which I can do this would be very much appreciated.

Thanks in advance.

Laura

JLM
11-11-2008, 01:49 PM
var days: Array = ['Monday', 'Tuesday','Wednesday','Thursday','Friday','Saturda y','Sunday'];

var currDay: String = 'Tuesday';
var datte: int = 11;
var count:int = 0;
for each ( var day:String in days ) {
if( currDay == day )
{
break;
} else
{
count++;
}
}
trace(count)
trace((datte)%7 )
var temp = (datte)%7-count-1;
trace(temp);
if( temp > 0 )
{
trace( days[ 7-temp ] );
}else{
trace( days[-temp]);
}

Laura_K
11-11-2008, 07:24 PM
JLM can you explain what exactly your code is doing so I can adapt it to my example.

So grateful for your reply.

pj-co
11-11-2008, 08:08 PM
Or you could just use getDay() on the 1st of the month.

Laura_K
11-11-2008, 08:40 PM
PJ-Co - How can I use Getday () on the first day of the month, as this will only work when it is actually the 1st as it reads from the date on your computer.

Sorry if I'm being stupid here.

pj-co
11-11-2008, 09:26 PM
PJ-Co - How can I use Getday () on the first day of the month, as this will only work when it is actually the 1st as it reads from the date on your computer.

Sorry if I'm being stupid here.

no worries -- here's how I might do it:



var today : Date = new Date();

var currentYear : Number = today.getYear();
var currentMonth : Number = today.getMonth();

// make a new date and pass the current year and current month
// from the today date object we created and a 1 for the 1st day

var firstOfTheMonth : Date = new Date ( currentYear, currentMonth, 1);

var firstWeekday : String = firstOfTheMonth.getDay();

trace(firstWeekday) //should be something like "Sunday" or "Monday"

JLM
11-12-2008, 03:06 AM
Laura

pj-co approach seems more transparent, mine was prob targeted solving the task in the way you asked the question rather than just solving the problem, but incase ideas in my code are useful to you in future, I will explain.

You already have day and date so I took thoughs as given and hardcoded values (and I checked different cases) you would set these with your date code instead.


var currDay: String = 'Tuesday';
var datte: int = 11;


The 'days' array helps me figure out with an 'for each' loop where in a week the current day is ( as Number ), by looping through, when the current day equals the day in the array we 'break' out of the loop, 'count' just stores which count in the array we reach.

Now '%' is related to modula division that is to say integer division, if you rem primary school division we always have a remainder after dividing ( before we really get into fractions )

So taking the current date (datte) we can find out the remainder of days, after dividing by weeks, so 11 days is 1 week and 4 days.

Now if we assumed a month starts on a 'currDay' (say Tuesday) then we can find the real day of week by offset tuesday by the remainder.

'temp' is the offset of the remainder non whole week days from the currDay's count value.

It is either negative or positive so we count up the week days by the offset or down the week days to find the start of month day.

My final code was based on some of this train of thought I have explained, but traces helps tweak the code to get a correct algorithm, it is not instant and sometimes there is a simpler approach!

__________________________________________________ _____
Notes:

'%' are really usefull in flash for instance you can check for remainder 0 so you could use a single 'for' loop for laying out a table of movies eg:

for( i:int = 0; i<tot; i++)
{
x+=10;
if( i%5 == 0 )
{
x==0;
y+=10;
}
mcs[i].x = x;
mcs[i].y = y;
}

Laura_K
11-12-2008, 07:12 PM
PJ -Co - that still isn't working its just returning the current day. It won't accept string in the second set of code, but thats not a problem Ive worked round that. What I wanted to do was to minus the current date from an array that contains the days of the week, but because its a number instead of counting back through my text array, its just giving me a minus figure.

When flash returns the day of the week its a number from 0 to 6.

To explain this further: I have an array containing S,M,T,W,T,F,S, this converts the current day, from a number, to a letter from the string. I also have a box on my screen that contains the current date, so today it contains 12. The way I thought I could return the first of the month is to count the current date amount back through my letter array. Currently the code I am using is returning a minus figure though instead of counting through my array, and I'm not sure what code to use to do this.

Your help has been great, I was totally stuck.

Laura

pj-co
11-14-2008, 02:11 AM
sorry the code i posted above is a bit buggy but you're probably done with this by now. Anyways here's what it should have been:




var weekdayNames : Array = [ "Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
];

var today : Date = new Date();
var currentYear : Number = today.getFullYear();
var currentMonth : Number = today.getMonth();

// make a new date and pass the current year and current month
// from the today date object we created and a 1 for the 1st day

var firstOfTheMonth : Date = new Date ( currentYear, currentMonth, 1);

var firstWeekday : Number = firstOfTheMonth.getDay();
var todayWeekday : Number = today.getDay();

//finally we can use the number representation of the weekday we want
//as the index of our weekdayNames array.

trace("today is a: ", weekdayNames [ todayWeekday ] );
trace("first day of the month was a: ", weekdayNames [ firstWeekday ] );