[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

javascript datediff function exclude weekends, return formatted date.

Posted on 2008-06-12
9
Medium Priority
?
1,361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 

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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

650 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