How do i calculate the difference in days between to date fields using Javascript?

1
0
-1

Dear BonitaSoft Community,

I have a little problem. I have two date field inputs and one textfield that should show the difference between the two dates.

The javascript goes like this:

function calculateDate(){
    var oldDate = new Date();
    var newDate = new Date();
    var days = 0;

    oldDate = document.getElementById("oldDate_value").value;
    newDate = document.getElementById("newDate_value").value;
    days = newDate.getTime()-oldDate.getTime();
    document.getElementById("days_value).values = days;

I have tried a different skript before where i just added 5 days to the days textfield, which worked. So the integration of the skript should work. But I think there is an error on the date calculation.

My Bonita BPM Community version is 6.3.7.

Kind regards, Pascal Petruch

2 answers

1
0
-1
This one is the BEST answer!

see here...

page down to the second answer - it is the best answer (127+ votes)

regards Seán

1
0
-1

Hello there,

thank you for the answer, but it doesen't work for my case. The problem here is that the Date object created by a date field in Bonita BPM has the format "20. November 2014". I have seen this stackoverflow post before. If I wanted to use this method, i would have to convert the 20. November 2014 to 11/20/2014. But this doesen't work.

I have tried to solve it with groovy too, but it doesent work either.

import static org.codehouse.groovy, runtime.DefaultGroovyMethods.*;
int ergebnis = 0;
use(groovy.time.TimeCategory) {
ergebnis = urlaub_bis-urlaub_von;
}
return ergebnis.days;

urlaub_von and urlaub_bis are the two imputs vom the date form fields. The result has to be integer, but there is a type-matching problem.

I don't know how to solve this, have tried it for hours.

Kind regards, Pascal

Comments

Submitted by Sean McP on Wed, 11/12/2014 - 18:00

Ok,

the problem here I think is not so much that you're using the Date/Calendar Widget but the fact that you're doing a calculation on the client.

Internally (on the server) Bonita will be using date and time as date and time, what you are trying to do is use the browsers representation, according to your locale settings. I understood this when you gave your example...

"20. November 2014"

this is your browsers "German locale" version of the date, which on my English system just shows

"20 November 2014"

Because you are using JavaScript on the form data (which is basic HTML), then you will have to convert it to a date format first before doing any calculations...JavaScript Date is quite flexible...

try this...from here

var d = new Date(dateString);

or unpackage the date using subString etc. using

var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

In - fact try it out here ...it works...change the date on the left to your exact format (20. November 2014) and then click the button and you will see JavaScipt honor the format, then you can do your calculations on var d.

You calculation simply needs to be here is a full set of function code:

function DaysDifference( date1, date2 ) {

var d1 = new date(date1);
var d2 = new date(date2);

  //Get 1 day in milliseconds
  var one_day=1000*60*60*24;

  // Convert both dates to milliseconds
  var date1_ms = d1.getTime();
  var date2_ms = d2.getTime();

  // Calculate the difference in milliseconds
  var difference_ms = date2_ms - date1_ms;
   
  // Convert back to days and return
  return Math.round(difference_ms/one_day);
}

var noOfDays = DaysDifference("20. November 2014", "25. November 2014");

best regards Sean

Notifications