Meaningless Drivel is fun!*
The moose likes HTML, CSS and JavaScript and the fly likes How to calculate date from two fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to calculate date from two fields" Watch "How to calculate date from two fields" New topic
Author

How to calculate date from two fields

vanan saravanan
Ranch Hand

Joined: Jun 02, 2006
Posts: 95
Hi, i have two textfields. Start date and end date. I use a date picker to enter the value into the textfields. The values are then stored into a mysql database. This part works fine. I got 3 fields in my database. A startdate, enddate and duration. My question is how do i find out the duration if i have two dates in two textfields.

Example

Textfield 1 : 13-06-2006
Textfield 2 : 16-06-2006
Duration = 3 days (How do i calculate this)
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Look into the Date() object in JavaScript. Plenty tutorials online with a simple search on Google.

Eric
vanan saravanan
Ranch Hand

Joined: Jun 02, 2006
Posts: 95
I tried this but it does not seem to work. "demoform" is my form name and "dc1" and "dc2" is the textfield name.


<script type="text/javascript">
<!--
function checkdate() {
var begDate = new Date(document.demoform.dc1.value);
var endDate = new Date(document.demoform.dc2.value);
var difDate = endDate.getTime() - begDate.getTime();
var days = difDate / (24*60*60*1000);
alert(days);
}
//-->
</script>
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
var difDate = endDate - begDate;


Eric
vanan saravanan
Ranch Hand

Joined: Jun 02, 2006
Posts: 95
I'm using DD-MM-YYYY format and the script does not seem to work. It only works if i use MM/DD/YY.

How do i change it.

Thank you for your help.
vanan saravanan
Ranch Hand

Joined: Jun 02, 2006
Posts: 95
Anyone able to help me with the above problem.
vanan saravanan
Ranch Hand

Joined: Jun 02, 2006
Posts: 95
Is there any other way to solve the problem of finding out the difference between days. I need to use DD-MM-YYYY as thats how the dates are saved in the database.

Database is mysql
Code is jsp
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
People do have lives away from the Ranch so you do not need to ask all the time. You have to change the format of the data. A simple split will do the job.

var d1 = document.forms[0].elementName.valuesplit("-");
var date1 = new Date(d1[2],parseInt(d1[1])-1,d1[0]);

Eric
vanan saravanan
Ranch Hand

Joined: Jun 02, 2006
Posts: 95
Problem solved. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to calculate date from two fields
 
Similar Threads
JTextField
jdbc
Format date using jsp to save into mysql DB
how to get duration starting date to ending date in visual jsf
Help needed in javascript coding