Hi, I want to make a form , in which I have 4 input Fields.

  1. Date1
  2. Date2
  3. Calender Days
  4. Working Days

When I enter date1 as 2011-04-07 and date2 as 2011-04-11

Then I require in Calender Days = 5 inclusive of date1 and date2

and Working days = 3 as SAT, SUN Off

Recommended Answers

All 10 Replies

You might benefit from checking JavaScript Date object.

I do not want to make it so complex, I will use Just 2 Table ,
1. Table1: Leaves Balance (Employee Id, Name, Casual, Annual, Medical , Type of Employee)
2. Table2: Leave Used ( Employee Id, Leave Type, Start Date, End Date , Calender Days, Working Days)

When Ever Person apply for Leaves all info inserted in Table1 and Table2 Updated.

Example:

Table1 (Leave Balance) Before Apply
Employee Id /// Name, /// Casual /// , Annual,/// Medical , /// Type of Employee
3 /// ABC /// 10 /// 14 10 Permanent

( If Person apply from 2011-04-07 to 2011-04-11 , working days are 3 and calender Days are 5 and Mark as Annual)


When Person Apply 2 Things Must Happened

Table1 (Leave Balance) After Apply (Annual Fields Updated from 14 to 11)
Employee Id Name, Casual , Annual, Medical , Type of Employee
3 ABC 10 11 10 Permanent

And Table2 (Leave Used ) After Apply ( 1 record inserted)

Employee Id, Leave Type, Start Date , End Date , Calender Days, Working Days
3 Anual 2011-04-07 2011-04-11 5 3

Only difficulty I feel to make a form.

Hi, I want to make a form , in which I have 4 input Fields.


• Date1
• Date2
• Calender Days (automatic get value after Date2 Entered)
• Working Days (automatic get value after Date2 Entered)

When I enter date1 as 2011-04-07 and date2 as 2011-04-11

Then I require in Calender Days = 5 inclusive of date1 and date2

and Working days = 3 as SAT, SUN Off

when user submit data, accept only from date, to_date, then in processing form , before inserting we can find working days using php/mysql syntax.

when user submit data, accept only from date, to_date, then in processing form , before inserting we can find working days using php/mysql syntax.

I have designed form , But I am confused,

Also , Is my idea is ok?

I want to do it using Jquery or Javascrip, in Below code I calculated calendar Days, How I find Working Days??? also I need to change date format from 2011,04,17 to 2011-04-17

<html>
<head>
<script type="text/javascript">
function days_between() {

   var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2011,04,01);
var secondDate = new Date(2011,04,17);
 
var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));

alert(diffDays+1);

}
</script>
</head>

<body>
<script type="text/javascript">
document.write(days_between());
</script>


</body>
</html>

I use this as a date picker in my form

http://jqueryui.com/demos/datepicker/

This code is working fine but there are 2 issues

If days_between(2011,04,01,2011,04,01); it shows 0 working days even on first April its Friday

if days_between(2011,04,02,2011,04,03); means Sat and Sun, it shows 2 Calendar days , OK but one working Day.

<html>

<head>

<script  lang=javascript>

function days_between(fryear,frmonth,frday,toyear,tomonth,today) 

{



   var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds

	var firstDate = new Date(fryear,frmonth,frday);

	var secondDate = new Date(toyear,tomonth,today);

	var loopDate=new Date();



	loopDate=firstDate;



	var caldays=0;

	var wkdays=0;

	for(i=0;loopDate<=secondDate;i++)

	{	

		caldays++;

		if(!(loopDate.toGMTString().substr(0,3).toUpperCase()=='SAT'

			 || loopDate.toGMTString().substr(0,3).toUpperCase()=='SUN'))

		{

			wkdays++;

		}

		loopDate =new Date(loopDate.getTime()+oneDay);

	}

	alert(caldays);

	alert(wkdays);

}

</script>

</head>



<body>

<script lang=javascript>

days_between(2011,04,01,2011,04,30);

</script>





</body>

</html>

working days = total days - weekend days. So we need to calculate how many weekend days there are.

I think we could use a loop something like this:

var total_days = x; //the difference you have already calculated between dates
var first_day = y;  //1-7 1 1 for monday, 7 for sunday, we need to know what it is.
var current_day = first_day; //again 1-7
var total_free_days = 0;

for(i = 0; i<=total_days; i++)
{
  if((current_day >= 6)  //saturday or sunday
  {
    total_free_days++;
    if(current day == 7) //if sunday, make it monday
      current_day = 1;
    else currrent_day = 7; //if saturnday, make it sunday
  }
  else current_day++;  //if workday, just make next day
  
}

I have not tested it but I think its something like this :)

Edit: the only problem is to find what is the first day - monday or what.. But maybe that date picker can get it

I just want to check why loop is not adding correct values, also if alert Loop date it shows 1 may , may be this is issue

oh, just checked your code, its similar to mine :) you wrote your code, while I was writing my one, so that's why I didn't see earlier :)

if

alert(firstDate);

it show one month plus

if

alert(firstDate);

it show one month plus

probably it is because it start counting montht from zero. If you enter 0, then it shows january


there are examples http://www.w3schools.com/jS/js_obj_date.asp

var myDate=new Date();
myDate.setFullYear(2010,0,14);
var today = new Date();

if (myDate>today)
  {
  alert("Today is before 14th January 2010");
  }
else
  {
  alert("Today is after 14th January 2010");
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.