• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to calculate date from two fields

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look into the Date() object in JavaScript. Plenty tutorials online with a simple search on Google.

Eric
 
vanan saravanan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
var difDate = endDate - begDate;


Eric
 
vanan saravanan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone able to help me with the above problem.
 
vanan saravanan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem solved. Thank you.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic