Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > Extensions and Plugins > Components

Reply
 
Thread Tools Rate Thread Display Modes
Old 01-20-2011, 07:32 PM   #1
bigjim
Registered User
 
Join Date: Nov 2010
Location: Miami FL
Posts: 15
Default [AS2] DateField component - Ignoring specific days in calculations

I'm hoping someone can help me with this issue.

I have a booking form with two datefield components named 'my_df' & 'my_df2'.
The required function is to calculate the number of days between the two selections and insert the result into a dynamic text field named 'message_txt'.
I have managed to get this function working, however I need the calculation to only include working days (ie. Monday - Friday).
I have disabled Saturday's and Sundays on the calendar itself by using:
Code:
my_df2.disabledDays = [0, 6];
But the calculation still takes Saturday and Sunday into account if, for example, the user selects a Friday - Monday range. (This calculates as 4 days, when it should calculate as 2).

The full code I currently have in place is as follows:
Code:
import mx.controls.*
var my_df:DateField;
calendar = new Object();
calendar.change = function(eventObj){
	date1 = my_df.selectedDate;
	year = date1.getFullYear();
	month = date1.getMonth();
	day = date1.getDate();
	my_df2.disabledRanges = [{rangeEnd: new Date(year, month, day)}];
	my_df2.disabledDays = [0, 6];
}

my_df.addEventListener("change", calendar);

var my_df2:DateField;
calendar2 = new Object();
calendar2.change = function(eventObj){
	date2 = my_df2.selectedDate;
}

my_df2.addEventListener("change", calendar2);

function calculateIt(){
	totalDays = date2 - date1;
	numDays = Math.floor((date2 - date1)/86400000);
	trace("totalDays var = " + totalDays);
	trace("numDays var = " + numDays);
	message_txt.text = " " + numDays + " Days";
}
bigjim is offline   Reply With Quote
Old 01-21-2011, 09:56 AM   #2
SDragon029
Determined Coder
 
Join Date: Nov 2010
Location: Ontario, Canada
Posts: 161
Send a message via MSN to SDragon029 Send a message via Skype™ to SDragon029
Default

Hey,
Not sure if this is exactly what your looking for but if not it should be close to it I think.
Code:
import mx.controls.*
var my_df:DateField;
calendar = new Object();
calendar.change = function(eventObj){
    date1 = my_df.selectedDate;
    year = date1.getFullYear();
    month = date1.getMonth();
    day = date1.getDate();
    my_df2.disabledRanges = [{rangeEnd: new Date(year, month, day)}];
    my_df2.disabledDays = [0, 6];
}

my_df.addEventListener("change", calendar);

var my_df2:DateField;
calendar2 = new Object();
calendar2.change = function(eventObj){
    date2 = my_df2.selectedDate;
    calculateIt();
}

my_df2.addEventListener("change", calendar2);

function calculateIt(){
    totalDays = date2 - date1;
    numDays = Math.floor(totalDays/86400000);
    //rounds the week number up or down depending on the number of days
    //that have been selected.
    if((numDays/7)<1) {
        numWeeks = Math.floor(numDays/7);
    } else {
        numWeeks = Math.ceil(numDays/7);
    }
    trace("totalDays var = " + totalDays);
    trace("numDays var = " + numDays);
    //minuses 2 days a week as long as the number of weeks is greater
    //than 0 weeks
    for(n=0;n<numWeeks;n++) {
        numDays-=2;
    }
    //minuses 2 days depending on what day of the week is selected as the start
    //day and what day is selected as the end day as long as the number of weeks
    //is equal to 0.
    if(numWeeks==0&&(date1.getUTCDay()==5||date1.getUTCDay()==6)&&(date2.getUTCDay()==1||date2.getUTCDay()==2||date2.getUTCDay()==3||date2.getUTCDay()==4||date2.getUTCDay()==5)) {
        if(!(date1.getUTCDay()==0&&date2.getUTCDay()==1)){
            numDays-=2;
        }
    }
    //adds a day or two to the number of days depending on if your starting
    //on a sunday and how many weeks there are so that for example if your
    //going from friday-monday in a streach of 4 days you get an output of
    //two days selected instead of just one. So basically it pads the
    //number of days to fix any errors that might occur from the above code
    //and makes it include the start date.
    if(numWeeks!=0) {
        if(date1.getUTCDay()==0) {
            numDays+=2;
        } else {
            numDays+=1;
        }
    } else {
        if(date1.getUTCDay()!=0) {
            numDays+=1;
        }
    }
    trace("numDays var = " + numDays);
    trace("numWeeks var = " + numWeeks);
    message_txt.text = " " + numDays + " Days";
}
The message gets set every time the end date changes.
Feel free to modify it or ask any questions about it.

Tim

Last edited by SDragon029; 01-21-2011 at 10:06 AM.
SDragon029 is offline   Reply With Quote
Old 01-21-2011, 01:47 PM   #3
bigjim
Registered User
 
Join Date: Nov 2010
Location: Miami FL
Posts: 15
Cool [AS2] DateField component - Ignoring specific days in calculations

Tim... That's awsome!

I haven't managed to test it with all possible combinations of date selection, but so far, it works perfectly.

Thank you for the detailed comments also... Means I won't just be copying and pasting. I actually (almost) understand what you did

Thank you again sir.

James.
bigjim is offline   Reply With Quote
Old 01-21-2011, 05:33 PM   #4
SDragon029
Determined Coder
 
Join Date: Nov 2010
Location: Ontario, Canada
Posts: 161
Send a message via MSN to SDragon029 Send a message via Skype™ to SDragon029
Default

Your quite welcome James. I figured you might need some comments to help understand the logic of what was going on there. Yeah I tried a lot of different combination's when I was coding it to see what was working and what wasn't and when something didn't work right I tried to fix it lol. Well at least with the comments you will hopefully learn something from what it's doing instead of just blindly copying it.
SDragon029 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:19 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.