?
Solved

a simple way to Subtract "dd/mm/yyyy" in javascript.

Posted on 2009-02-15
7
Medium Priority
?
628 Views
Last Modified: 2012-05-06
I need to subtract string date from another string date, the the string format is:
"dd/mm/yyyy", this needs to be done in Javascript,
I'm looking for a simple and efficient way to do this.
0
Comment
Question by:RahnLev
7 Comments
 
LVL 15

Expert Comment

by:fsze88
ID: 23645205
you could use
re = new RegExp("[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}");
depend on your goal whether is able one decimal of day and month

<script language="javascript">
 
var strdate = '12:12:00 pm 12/12/2009 abc testing';
re = new RegExp("[0-9]{2}/[0-9]{2}/[0-9]{4}"); 
var newstr = strdate.replace(re, "");
document.write("newstr : " + newstr);
</script>

Open in new window

0
 

Author Comment

by:RahnLev
ID: 23645275
I mean that I beed to get  the value of:
15/02/2009 - 13/02/2009 = 2

2 is what I need.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 23645386
var strDate1 = "13/11/2009"
 var strDate2 = "15/11/2009"
 var arrDate1 = strDate1.split("/");
 var arrDate2 = strDate2.split("/");
 var d1 = new Date(arrDate1[2],arrDate1[1]-1,arrDate1[0],0,0,0)
 var d2 = new Date(arrDate2[2],arrDate2[1]-1,arrDate2[0],0,0,0)
var diffInMilli = d2.getTime()-d1.getTime();
var diffInDays = diffInMilli/(24*60*60*1000)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:RahnLev
ID: 23645455
Does not let me use the new key word.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 23645576
What do you mean?

Here is a function

function diff(strDate1,strDate2) {
  var arrDate1 = strDate1.split("/");
  var arrDate2 = strDate2.split("/");
  var d1 = new Date(arrDate1[2],arrDate1[1]-1,arrDate1[0],0,0,0)
  var d2 = new Date(arrDate2[2],arrDate2[1]-1,arrDate2[0],0,0,0)
  var diffInMilli = d2.getTime()-d1.getTime();
  var diffInDays = diffInMilli/(24*60*60*1000)
  return diffInDays
}
var beginDate = "13/11/2009"
var endDate = "15/11/2009"
alert(diff(beginDate,endDate))

Open in new window

0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23645606
Try this one from http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20483408.html
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function dateDifference(strDate1,strDate2){
     datDate1= Date.parse(strDate1);
     datDate2= Date.parse(strDate2);
     alert((datDate2-datDate1)/(24*60*60*1000))
     
}
//-->
</SCRIPT>
 
</HEAD>
<BODY>
 
<FORM action="" method=POST id=form1 name=form1><P>&nbsp;</P>
<INPUT type="text" id=text1 name=text1>
<INPUT type="text" id=text2 name=text2>
<INPUT type="button" value="Button" id=button1 name=button1 onClick=dateDifference(this.form.text1.value,this.form.text2.value)>
</FORM>
</BODY>
</HTML>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 23647730
Sunithair, Date.parse does not work with european formatted dates

javascript:alert(new Date(Date.parse("13/11/2009")))
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

839 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