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
Solved

javascript datediff function exclude weekends, return formatted date.

Posted on 2008-06-12
9
1,353 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dynamic Dropdowns 15 32
how can i make 2 columns? 4 36
100% tall div not scrollable on iPhone 3 20
How to make button inactive until I fill in all the boxes. 11 18
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

790 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