473,378 Members | 1,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Weekday difference between two given days

RamananKalirajan
608 512MB
Can any one please help me out in calculating the no of weekdays between two givendays using Javascripts I tried in many way but I cant get the solution. The date format I am using is DD-MON-YYYY. Please help me out
Mar 10 '08 #1

✓ answered by RamananKalirajan

/*
Date calculations from user input can be tricky-
the safest method is to have your visitor choose from a calendar or
a set of drop downs. If you let them type in their own dates,
make sure you have actual dates to work with before you use them.

I simplified your code here, but to pay for it
you will need to come up with a Holidays object on your own-
you need to subtract week-day holidays from the business days total.
(I made it optional here, but you will need it for accurate business day counts)
The particular holidays depend on the business.
Lucky for you that Easter always falls on a Sunday....

I made it a method of the Date object, but you can make it a method of
any object you have handy, or even a global function- whatever works.
*/
Expand|Select|Wrap|Line Numbers
  1. Date.bizDays= function(d1,d2,skipholidays){
  2.     d1= Date.fromInput(d1);
  3.     d2= Date.fromInput(d2);
  4.  
  5.     var wd1= d1.getDay();
  6.     var wd2= d2.getDay();
  7.  
  8.     var interval= Math.abs(d1-d2);
  9.     var days= Math.floor(interval/8.46e7);
  10.     var tem= days%7;
  11.     var weeks= Math.floor(days/7);
  12.     if(wd1== 6) tem-= 2;
  13.     else if(wd1== 0) tem-= 1;
  14.     if(wd2== 0) tem-= 1;
  15.  
  16.     if(skipholidays=== true && Holidays=== 'object'){
  17.         var skip= new Holidays(d1,d2).length;
  18.         tem-= skip;
  19.     }
  20.     return weeks*5+tem;
  21. }
Expand|Select|Wrap|Line Numbers
  1. Date.fromInput= function(d){
  2.     try{
  3.         if(typeof d== 'number')d=new Date(d);
  4.         if(typeof d== 'string') d= new Date(Date.parse(d));
  5.         if(d.getDay()) return d;
  6.     }
  7.     catch(er){
  8.         throw('Bad Date!')
  9.     }
  10. }
//test - the format of the string depends on the OS language settings
//you can pass actual Date Objects, or time stamps as well as strings
alert(Date.bizDays('March 11, 2008','April 15,2008')+' business days');
//error test
//Date.bizDays()

Thank You Sir, But i found another way to do this that was pretty cool I tried it and it is working well but it will take more time for high date difference. The following is the code

Expand|Select|Wrap|Line Numbers
  1. <Html>
  2. <head>
  3. <script language="javascript">
  4. function changeVal(month)
  5. {
  6. if(month=="jan"||month=="Jan"||month=="JAN")
  7.       month=0;
  8. if(month=="feb"||month=="Feb"||month=="FEB")
  9.       month=1;
  10. if(month=="mar"||month=="Mar"||month=="MAR")
  11.       month=2;
  12. if(month=="apr"||month=="Apr"||month=="APR")
  13.       month=3;
  14. if(month=="may"||month=="May"||month=="MAY")
  15.       month=4;
  16. if(month=="jun"||month=="Jun"||month=="JUN")
  17.       month=5;
  18. if(month=="jul"||month=="Jul"||month=="JUL")
  19.       month=6;
  20. if(month=="aug"||month=="Aug"||month=="AUG")
  21.       month=7;
  22. if(month=="sep"||month=="Sep"||month=="SEP")
  23.       month=8;
  24. if(month=="oct"||month=="Oct"||month=="OCT")
  25.       month=9;
  26. if(month=="nov"||month=="Nov"||month=="NOV")
  27.       month=10;
  28. if(month=="dec"||month=="Dec"||month=="DEC")
  29.       month=11;
  30.  
  31. return(month);
  32. }
  33. function findDiff()
  34. {
  35. var day1, day2;
  36. var month1, month2;
  37. var year1, year2;
  38.  
  39. value1 = document.getElementById('stDate').value;
  40. value2 = document.getElementById('endDate').value;
  41.  
  42. day1 = value1.substring (0, value1.indexOf ("-"));
  43. month1 = value1.substring (value1.indexOf ("-")+1, value1.lastIndexOf ("-"));
  44. month1 = changeVal(month1);
  45. year1 = value1.substring (value1.lastIndexOf ("-")+1, value1.length);
  46.  
  47. day2 = value2.substring (0, value2.indexOf ("-"));
  48. month2 = value2.substring (value2.indexOf ("-")+1, value2.lastIndexOf ("-"));
  49. month2 = changeVal(month2);
  50. year2 = value2.substring (value2.lastIndexOf ("-")+1, value2.length); 
  51.  
  52. var startDate = new Date(year1,month1,day1);
  53. var endDate = new Date(year2,month2,day2);
  54. var wrkdays=0;
  55.  
  56. while(startDate.getTime()<=endDate.getTime())
  57. {
  58.      wrkdays++;       
  59.     if(startDate.getDay()==0)
  60.    {
  61.     wrkdays--;
  62.    }
  63.    else if(startDate.getDay()==6)
  64.   {
  65.     wrkdays--;
  66.   }
  67.   else
  68.   {
  69.   }
  70.       var d = new Date(startDate.getTime() + 86400000);
  71.    startDate=d;
  72. }
  73.       alert("Total Workdays = "+wrkdays);
  74. }
  75.  
  76. </script>
  77. </head>
  78. <body>
  79. <input type="text" id="stDate"><br/>
  80. <input type="text" id="endDate"><br/>
  81. <input type="button" value="calculate" onclick="findDiff()">
  82. </body>
  83. </html>
  84.  
Anyhow thank you for your reply. i will try that too. Thank you

6 2543
RamananKalirajan
608 512MB
Hello firends, here i give a code that finds the weekday between two dates. But the problem is the code just subtracts only two days (i.e. a saturday and Sunday). can any one please solve this.

The Code:

[HTML]<html>
<head>
<script language="javascript">
function diffDate()
{
var day1, day2;
var month1, month2;
var year1, year2;

value1 = document.getElementById('stDate').value;
value2 = document.getElementById('endDate').value;

day1 = value1.substring (0, value1.indexOf ("-"));
month1 = value1.substring (value1.indexOf ("-")+1, value1.lastIndexOf ("-"));
year1 = value1.substring (value1.lastIndexOf ("-")+1, value1.length);

day2 = value2.substring (0, value2.indexOf ("-"));
month2 = value2.substring (value2.indexOf ("-")+1, value2.lastIndexOf ("-"));
year2 = value2.substring (value2.lastIndexOf ("-")+1, value2.length);

date1 = year1+"/"+month1+"/"+day1;
date2 = year2+"/"+month2+"/"+day2;

firstDate = Date.parse(date1);
secondDate= Date.parse(date2);

var ddate1 = new Date();
var ddate2 = new Date();

ddate1.setUTCMilliseconds(firstDate);
ddate2.setUTCMilliseconds(secondDate);

var msPerDay = 24 * 60 * 60 * 1000
var dbd = Math.round((secondDate.valueOf()-firstDate.valueOf())/ msPerDay)+1;
alert(dbd);

var iWeeks, iDateDiff, iAdjust = 0;
var iWeekday1 = ddate1.getDay(); // day of week
var iWeekday2 = ddate2.getDay();
iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
if ((iWeekday1 > 5) && (iWeekday2 > 5))
iAdjust = 1; // adjustment if both days on weekend
iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2; // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
iWeeks = Math.floor((ddate2.getTime() - ddate1.getTime()) / 604800000);
if (iWeekday1 <= iWeekday2)
{
iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
}
else
{
iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
}

iDateDiff -= iAdjust // take into account both days on weekend
iDateDiff =iDateDiff +1;
alert("The Working Days are "+iDateDiff);
}
</script>
</head>
<body>
<input type="text" id="stDate"><br/>
<input type="text" id="endDate"><br/>
<input type="button" value="calculate" onclick="diffDate()">
</body>
</html>
[/HTML]
Please Help Me out
Mar 10 '08 #2
acoder
16,027 Expert Mod 8TB
Threads merged. Please do not double post.

Also remember to use [code] tags when posting code.
Mar 10 '08 #3
mrhoo
428 256MB
/*
Date calculations from user input can be tricky-
the safest method is to have your visitor choose from a calendar or
a set of drop downs. If you let them type in their own dates,
make sure you have actual dates to work with before you use them.

I simplified your code here, but to pay for it
you will need to come up with a Holidays object on your own-
you need to subtract week-day holidays from the business days total.
(I made it optional here, but you will need it for accurate business day counts)
The particular holidays depend on the business.
Lucky for you that Easter always falls on a Sunday....

I made it a method of the Date object, but you can make it a method of
any object you have handy, or even a global function- whatever works.
*/
Expand|Select|Wrap|Line Numbers
  1. Date.bizDays= function(d1,d2,skipholidays){
  2.     d1= Date.fromInput(d1);
  3.     d2= Date.fromInput(d2);
  4.  
  5.     var wd1= d1.getDay();
  6.     var wd2= d2.getDay();
  7.  
  8.     var interval= Math.abs(d1-d2);
  9.     var days= Math.floor(interval/8.46e7);
  10.     var tem= days%7;
  11.     var weeks= Math.floor(days/7);
  12.     if(wd1== 6) tem-= 2;
  13.     else if(wd1== 0) tem-= 1;
  14.     if(wd2== 0) tem-= 1;
  15.  
  16.     if(skipholidays=== true && Holidays=== 'object'){
  17.         var skip= new Holidays(d1,d2).length;
  18.         tem-= skip;
  19.     }
  20.     return weeks*5+tem;
  21. }
Expand|Select|Wrap|Line Numbers
  1. Date.fromInput= function(d){
  2.     try{
  3.         if(typeof d== 'number')d=new Date(d);
  4.         if(typeof d== 'string') d= new Date(Date.parse(d));
  5.         if(d.getDay()) return d;
  6.     }
  7.     catch(er){
  8.         throw('Bad Date!')
  9.     }
  10. }
//test - the format of the string depends on the OS language settings
//you can pass actual Date Objects, or time stamps as well as strings
alert(Date.bizDays('March 11, 2008','April 15,2008')+' business days');
//error test
//Date.bizDays()
Mar 10 '08 #4
RamananKalirajan
608 512MB
/*
Date calculations from user input can be tricky-
the safest method is to have your visitor choose from a calendar or
a set of drop downs. If you let them type in their own dates,
make sure you have actual dates to work with before you use them.

I simplified your code here, but to pay for it
you will need to come up with a Holidays object on your own-
you need to subtract week-day holidays from the business days total.
(I made it optional here, but you will need it for accurate business day counts)
The particular holidays depend on the business.
Lucky for you that Easter always falls on a Sunday....

I made it a method of the Date object, but you can make it a method of
any object you have handy, or even a global function- whatever works.
*/
Expand|Select|Wrap|Line Numbers
  1. Date.bizDays= function(d1,d2,skipholidays){
  2.     d1= Date.fromInput(d1);
  3.     d2= Date.fromInput(d2);
  4.  
  5.     var wd1= d1.getDay();
  6.     var wd2= d2.getDay();
  7.  
  8.     var interval= Math.abs(d1-d2);
  9.     var days= Math.floor(interval/8.46e7);
  10.     var tem= days%7;
  11.     var weeks= Math.floor(days/7);
  12.     if(wd1== 6) tem-= 2;
  13.     else if(wd1== 0) tem-= 1;
  14.     if(wd2== 0) tem-= 1;
  15.  
  16.     if(skipholidays=== true && Holidays=== 'object'){
  17.         var skip= new Holidays(d1,d2).length;
  18.         tem-= skip;
  19.     }
  20.     return weeks*5+tem;
  21. }
Expand|Select|Wrap|Line Numbers
  1. Date.fromInput= function(d){
  2.     try{
  3.         if(typeof d== 'number')d=new Date(d);
  4.         if(typeof d== 'string') d= new Date(Date.parse(d));
  5.         if(d.getDay()) return d;
  6.     }
  7.     catch(er){
  8.         throw('Bad Date!')
  9.     }
  10. }
//test - the format of the string depends on the OS language settings
//you can pass actual Date Objects, or time stamps as well as strings
alert(Date.bizDays('March 11, 2008','April 15,2008')+' business days');
//error test
//Date.bizDays()

Thank You Sir, But i found another way to do this that was pretty cool I tried it and it is working well but it will take more time for high date difference. The following is the code

Expand|Select|Wrap|Line Numbers
  1. <Html>
  2. <head>
  3. <script language="javascript">
  4. function changeVal(month)
  5. {
  6. if(month=="jan"||month=="Jan"||month=="JAN")
  7.       month=0;
  8. if(month=="feb"||month=="Feb"||month=="FEB")
  9.       month=1;
  10. if(month=="mar"||month=="Mar"||month=="MAR")
  11.       month=2;
  12. if(month=="apr"||month=="Apr"||month=="APR")
  13.       month=3;
  14. if(month=="may"||month=="May"||month=="MAY")
  15.       month=4;
  16. if(month=="jun"||month=="Jun"||month=="JUN")
  17.       month=5;
  18. if(month=="jul"||month=="Jul"||month=="JUL")
  19.       month=6;
  20. if(month=="aug"||month=="Aug"||month=="AUG")
  21.       month=7;
  22. if(month=="sep"||month=="Sep"||month=="SEP")
  23.       month=8;
  24. if(month=="oct"||month=="Oct"||month=="OCT")
  25.       month=9;
  26. if(month=="nov"||month=="Nov"||month=="NOV")
  27.       month=10;
  28. if(month=="dec"||month=="Dec"||month=="DEC")
  29.       month=11;
  30.  
  31. return(month);
  32. }
  33. function findDiff()
  34. {
  35. var day1, day2;
  36. var month1, month2;
  37. var year1, year2;
  38.  
  39. value1 = document.getElementById('stDate').value;
  40. value2 = document.getElementById('endDate').value;
  41.  
  42. day1 = value1.substring (0, value1.indexOf ("-"));
  43. month1 = value1.substring (value1.indexOf ("-")+1, value1.lastIndexOf ("-"));
  44. month1 = changeVal(month1);
  45. year1 = value1.substring (value1.lastIndexOf ("-")+1, value1.length);
  46.  
  47. day2 = value2.substring (0, value2.indexOf ("-"));
  48. month2 = value2.substring (value2.indexOf ("-")+1, value2.lastIndexOf ("-"));
  49. month2 = changeVal(month2);
  50. year2 = value2.substring (value2.lastIndexOf ("-")+1, value2.length); 
  51.  
  52. var startDate = new Date(year1,month1,day1);
  53. var endDate = new Date(year2,month2,day2);
  54. var wrkdays=0;
  55.  
  56. while(startDate.getTime()<=endDate.getTime())
  57. {
  58.      wrkdays++;       
  59.     if(startDate.getDay()==0)
  60.    {
  61.     wrkdays--;
  62.    }
  63.    else if(startDate.getDay()==6)
  64.   {
  65.     wrkdays--;
  66.   }
  67.   else
  68.   {
  69.   }
  70.       var d = new Date(startDate.getTime() + 86400000);
  71.    startDate=d;
  72. }
  73.       alert("Total Workdays = "+wrkdays);
  74. }
  75.  
  76. </script>
  77. </head>
  78. <body>
  79. <input type="text" id="stDate"><br/>
  80. <input type="text" id="endDate"><br/>
  81. <input type="button" value="calculate" onclick="findDiff()">
  82. </body>
  83. </html>
  84.  
Anyhow thank you for your reply. i will try that too. Thank you
Mar 11 '08 #5
acoder
16,027 Expert Mod 8TB
Thank You Sir, But i found another way to do this that was pretty cool I tried it and it is working well but it will take more time for high date difference. The following is the code
It can definitely be improved, e.g. the ChangeVal() function can be three/four lines:
Expand|Select|Wrap|Line Numbers
  1. function ChangeVal(month) {
  2.   var months = ["JAN","FEB","MAR",...,"DEC"];
  3.   for (i = 0; i < months.length; i++) {
  4.     if (month.toUpperCase() == months[i]) return i;
  5.   }
  6.   return -1;
  7. }
Mar 11 '08 #6
RamananKalirajan
608 512MB
It can definitely be improved, e.g. the ChangeVal() function can be three/four lines:
Expand|Select|Wrap|Line Numbers
  1. function ChangeVal(month) {
  2.   var months = ["JAN","FEB","MAR",...,"DEC"];
  3.   for (i = 0; i < months.length; i++) {
  4.     if (month.toUpperCase() == months[i]) return i;
  5.   }
  6.   return -1;
  7. }

Thank you for your suggestion sir I will make use of it
Regards
Ramanan
Mar 12 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: dr. zoidberg | last post by:
Hello, I have this: Mon, Tue, Wed, Thu, Fri, Sat, Sun created as links. What I would like to do is to associate days with its current week dates, for example: Mon (05.01.2004); Tue...
2
by: David Morgan | last post by:
Hi Hopefully the subject says it all. Given a weekday, (1 to 7) and an occurrence with in a month, (1 to 4), I need to ascertain the date on which it first occurs in any given month and year. ...
2
by: Chumley the Walrus | last post by:
I can't locate a weekday function in asp.net, whereas I just want to grab the weekday associated with a given date. I'm using this date format {0:MM/dd/yyyy} in references in my code, just need to...
1
by: Kd | last post by:
I am currently using a form with Weekdays Mon Tues Wed Thur Fri This is generated by a table that the days are being entered manually I would like to create a form that the days updated...
5
by: Simon Dean | last post by:
Probably being a little thick here, but when you subtract one date away from another, how do you convert the resultant value into a number of days... I guess I could easily / 60 / 60 / 24... but...
14
by: Divit | last post by:
I want my code to tell me that (today) April 12th is the second wednesday of the month. And only thing I can find is the function to tell me what date belongs to the 2nd day in a given month and...
6
by: kevinjwilson | last post by:
I am trying to get the date difference between two dates but I don't want the function to include weekends in the calculation. Does anyone have an idea on how to make this work?
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
4
by: dgmoore | last post by:
I've hit a snag - I know this is easy, but the logic is escaping me. I need to set criteria in a query to find dates in a date field that lie between Tuesday of the current week and Tuesday of the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.