Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JAVASCRIPT EQUIVALENT OF "DATEDIFF" IN ASP?

Posted on 2004-04-01
7
Medium Priority
?
685 Views
Last Modified: 2008-02-01
Hi experts,

I need to compare 2 dates in javascript to see which one is earlier. In ASP I would do this:

value = dateDiff("d","10/04/2004","09/04/2004")

This would produce a value of "-1"


value = dateDiff("d","09/04/2004","10/04/2004")

This would produce a value of "1"


I need to do the same thing in Javascript. Does anybody have an example of how I can do this?




PJORDANNA






0
Comment
Question by:pjordanna
[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
7 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 10731737
Like this:

<script>
value = dateDiff("d","04/10/2004","04/09/2004");
alert(value)

value = dateDiff("d","04/09/2004","04/10/2004");
alert(value);


function dateDiff(dType, date1, date2){
  divider = 1000;
  if(dType=="d") divider = 24*60*60*1000;
  return ((new Date(date2))-(new Date(date1)))/divider;
}
</script>

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10731765
Or with date format adaption:

<script>
value = dateDiff("d","10/04/2004","09/04/2004");
alert(value)

value = dateDiff("d","09/04/2004","10/04/2004");
alert(value);


function dateDiff(dType, date1, date2){
  divider = 1000;
  if(dType=="d") divider = 24*60*60*1000;
  return (getDate(date2)-getDate(date1))/divider;
}

function getDate(dateValue){
  dPart = dateValue.split("/");
  return new Date(dPart[2], dPart[1], dPart[0]);
}
</script>

0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10731772
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Accepted Solution

by:
GwynforWeb earned 2000 total points
ID: 10731807
try this

<script>
function dateDiff(d1,d2){
D1=new Date(d1)
D2=new Date(d2)
if (D1.getTime() > D2.getTime()) return -1
    else return 1
}


alert( dateDiff("10/04/2004","09/04/2004") )
alert( dateDiff("09/04/2004","10/04/2004") )
</script>
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10731838
Here also for hours minutes and seconds:

<script>
value = dateDiff("d","10/04/2004","09/04/2004");
alert(value)

value = dateDiff("d","09/04/2004","10/04/2004");
alert(value);


function dateDiff(dType, date1, date2){
  interval = 1000;
  switch(dType.toLowerCase()){
    case("d"): interval = 24*60*60*1000; break;
    case("h"): interval = 60*60*1000; break;
    case("m"): interval = 60*1000; break;
  }
  return (getDate(date2)-getDate(date1))/interval;
}

function getDate(dateValue){
  dPart = dateValue.split("/");
  return new Date(dPart[2], dPart[1], dPart[0]);
}
</script>

0
 

Author Comment

by:pjordanna
ID: 10732576
GwynforWeb,

That's perfect. Exeactly what I needed.




PJORDANNA
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10739637
Thanks, GfW
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

722 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