Solved

javascript datediff function exclude weekends, return formatted date.

Posted on 2008-06-12
9
1,346 Views
Last Modified: 2008-06-13
Hi Experts,

I'm trying to build a function [called nDate] that'll give the following output when supplied with an integer.

Todays date is 12th June 2008 (or 06/12/2008 as javascript seems to prefer)

nDate(0) returns "20080612"  (today - thursday)
nDate(1) returns "20080611"  (yesterday - wednesday)
nDate(2) returns "20080610"  (tuesday
nDate(3) returns "20080609"  (monday) [need to exclude saturday and sundays from calculation]
nDate(4) returns "20080606"  (friday)
nDate(5) returns "20080605"  (thursday)


I'll make a start, but any pointers would be highly appreciated!

Cheers
Jon
0
Comment
Question by:jondanger
  • 4
  • 3
  • 2
9 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 21767076
What about today being sat or sun?

You need something like

var d = new Date();
d.setDate(d.getDate()-1)
0
 
LVL 11

Expert Comment

by:JohnSixkiller
ID: 21767157
Hi, if you are only trying to skip actual weekend and "move" to previous workday:
function nDate(nDays){

  var date = new Date();

  date.setDate(date.getDate() - nDays);

  if(date.getDay() == 0) date.setDate(date.getDate() - 2); //Sunday

  else if(date.getDay() == 6) date.setDate(date.getDate() - 1); //Sat.
 

  return date;

}

Open in new window

0
 

Author Comment

by:jondanger
ID: 21769541
Thanks JohnSixKiller, that gave me a base to build on, but your solution only works for this and the previous week.  I've written a function that loops backwards until finished.

can someone make any improvements to the script, it looks a bit cumbersome to me.
<Script>
 

function nDate(nDays){

  var date = new Date();
 

  for (var j=0; j<nDays; j++){

    if(date.getDay() == 1){

      date.setDate(date.getDate() - 3); //Sunday

    } else if (date.getDay() == 0) {

      date.setDate(date.getDate() - 2); //Saturday

    } else {

      date.setDate(date.getDate() - 1)

    }

  }
 

  var date1 = date.getDate().toString();

  var date2 = date.getMonth() + 1

  var date2 = date2.toString();

  var date3 = date.getFullYear().toString();
 

  if (date1.length == 1){ date1 = "0" + date1 }

  if (date2.length == 1){ date2 = "0" + date2 }
 

  date = date3 + date2 + date1 
 

  return date;

}
 
 
 

for (var i=0; i < 50; i++){

document.write(i + " returns   " + nDate(i) + "<br/>" );

}

</script>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 21769617
His script works for all weeks

<script>
function nDate(nDays){
  var date = new Date();
  date.setDate(date.getDate() - nDays);
  if(date.getDay() == 0) date.setDate(date.getDate() - 2); //Sunday
  else if(date.getDay() == 6) date.setDate(date.getDate() - 1); //Sat.
  var mm = date.getMonth();
  var dd = date.getDate();
  if (mm<10) mm= "0"+mm;
  if (dd<10) dd= "0"+dd;
  return date.getFullYear()+''+mm+''+dd;
}


for (var i=0; i < 50; i++){
document.write(i + " returns   " + nDate(i) + "<br/>" );
}

</script>


It returns friday when passed  friday, saturday or sunday
Yours returns something else...
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:jondanger
ID: 21769803
my script produces the correct output. i haven't been clear enough with my question.

I want to go back "n" working days (exclude weekends from being counted as previous days)


Output should be this
nDate(0) returns "20080612"  (today - thursday)
nDate(1) returns "20080611"  (yesterday - wednesday)
nDate(2) returns "20080610"  (tuesday
nDate(3) returns "20080609"  (monday) [need to exclude saturday and sundays from calculation]
nDate(4) returns "20080606"  (friday)
nDate(5) returns "20080605"  (thursday)
nDate(6) returns "20080604"  (wednesday)
nDate(7) returns "20080603"  (tuesday)
nDate(8) returns "20080602"  (monday) [need to exclude saturday and sundays from calculation]
nDate(9) returns "20080530"  (friday)
nDate(10) returns "20080530"  (thursday)
................................

can anyone improve upon this code?



<Script>

 

function nDate(nDays){

  var date = new Date();

 

  for (var j=0; j<nDays; j++){

    if(date.getDay() == 1){

      date.setDate(date.getDate() - 3); //Sunday

    } else if (date.getDay() == 0) {

      date.setDate(date.getDate() - 2); //Saturday

    } else {

      date.setDate(date.getDate() - 1)

    }

  }

 

  var date1 = date.getDate().toString();

  var date2 = date.getMonth() + 1

  var date2 = date2.toString();

  var date3 = date.getFullYear().toString();

 

  if (date1.length == 1){ date1 = "0" + date1 }

  if (date2.length == 1){ date2 = "0" + date2 }

 

  date = date3 + date2 + date1 

 

  return date;

}

 

 

 

for (var i=0; i < 50; i++){

document.write(i + " returns   " + nDate(i) + "<br/>" );

}

</script>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 21770349
I do not get your script.
In any case you can change


 
  var date1 = date.getDate().toString();
  var date2 = date.getMonth() + 1
  var date2 = date2.toString();
  var date3 = date.getFullYear().toString();
 
  if (date1.length == 1){ date1 = "0" + date1 }
  if (date2.length == 1){ date2 = "0" + date2 }
 
  date = date3 + date2 + date1

to



  var mm = date.getMonth();
  var dd = date.getDate();
  if (mm<10) mm= "0"+mm;
  if (dd<10) dd= "0"+dd;
  return date + ':'+ date.getFullYear()+''+mm+''+dd;
0
 
LVL 11

Accepted Solution

by:
JohnSixkiller earned 500 total points
ID: 21770744
It was my first Idea. To simplify:

PS: what about mplungjan's first question?
function nDate(nDays){

  var weeks = Math.floor(nDays / 5);

      nDays += weeks * 2;

  var date = new Date();

  date.setDate(date.getDate() - nDays);
 

  /* if target date is still  weekend */

  if(date.getDay() == 0) date.setDate(date.getDate() - 2); //Sunday

  else if(date.getDay() == 6) date.setDate(date.getDate() - 1); //Sat.
 

  var day  = date.getDate();

  var mon  = date.getMonth() + 1

  var year = date.getFullYear();

 

  if(day < 10) day = "0" + day; 

  if(mon < 10) mon = "0" + mon;

  var dateString = year + ":" + mon + ":" +day;

  

  return dateString;

}

Open in new window

0
 

Author Comment

by:jondanger
ID: 21776579
that's spot on, thanks very much.

The script will never be run at weekends but have copied lines 8 and 9 into 4/5 line in case.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 21777009
Hmm, that was a waste of my time...
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now