javascript datediff function exclude weekends, return formatted date.

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
jondangerAsked:
Who is Participating?
 
JohnSixkillerConnect With a Mentor Commented:
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
 
Michel PlungjanIT ExpertCommented:
What about today being sat or sun?

You need something like

var d = new Date();
d.setDate(d.getDate()-1)
0
 
JohnSixkillerCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
jondangerAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
jondangerAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
jondangerAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
Hmm, that was a waste of my time...
0
All Courses

From novice to tech pro — start learning today.