Link to home
Create AccountLog in
Avatar of jondanger
jondanger

asked on

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
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

What about today being sat or sun?

You need something like

var d = new Date();
d.setDate(d.getDate()-1)
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

Avatar of jondanger
jondanger

ASKER

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

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...
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

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;
ASKER CERTIFIED SOLUTION
Avatar of JohnSixkiller
JohnSixkiller
Flag of Czechia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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.
Hmm, that was a waste of my time...