Page 1 of 1

Add Days to date (Solved)

Posted: Mon Jun 18, 2018 3:14 pm
by Leenomeedo
Hi;
I am new in processmaker I wanna to add given days to date ; for example in leave request user chose starting date from calender and then type for example 30 days for number of days request how can calculate this days to be added to calender to be automatic display the end of the leave date.

Re: Add Days to date

Posted: Mon Jun 18, 2018 8:48 pm
by amosbatto
If you want to do this in a trigger with PHP:
Code: Select all
@@endDate = date("Y-m-d", strtotime(@@startDate, @%daysToAdd));
Where "startDate" is the variable associated with a datetime field and "daysToAdd" is the variable associated with a text field in a previous DynaForm and "endDate" is the variable associated with a datetime in a future DynaFom.

If you want to do this with JavaScript in a DynaForm, then you can use the following code:
Code: Select all
function calculateEndDate(startDate, daysToAdd) {
  if (startDate == '' || daysToAdd == '' || parseFloat(daysToAdd) == NaN) {
    $("#endDate").setValue('');
  }
  
  var end = moment(startDate).add(daysToAdd, 'days').format("YYYY-MM-DD");
  $("#endDate").setValue(end);
}

$("#startDate").setOnchange(function(newDate, oldDate) {
  calculateEndDate( newDate, $("#daysToAdd").getValue() );
})[attachment=0]add days to date.json[/attachment]

$("#daysToAdd").setOnchange(function(newDays, oldDays) {
  calculateEndDate( $("#startDate").getValue(), newDays );
})
Where "startDate" and "endDate" are the IDs of datetime fields and "daysToAdd" is a text field. To see an example of this code, import the following DynaForm:
(4.59 KiB) Downloaded 315 times

Re: Add Days to date

Posted: Tue Jun 19, 2018 1:45 am
by Leenomeedo
Thank you for your support

Re: Add Days to date

Posted: Tue Jun 19, 2018 5:39 am
by Leenomeedo
Ok excuse me so if I follow the way of php trigger where exactly run this trigger to calculate this filed I want to implement this issue before I submit the form and exactly after typing the days to add
amosbatto wrote: Mon Jun 18, 2018 8:48 pm If you want to do this in a trigger with PHP:
Code: Select all
@@endDate = date("Y-m-d", strtotime(@@startDate, @%daysToAdd));
Where "startDate" is the variable associated with a datetime field and "daysToAdd" is the variable associated with a text field in a previous DynaForm and "endDate" is the variable associated with a datetime in a future DynaFom.

If you want to do this with JavaScript in a DynaForm, then you can use the following code:
Code: Select all
function calculateEndDate(startDate, daysToAdd) {
  if (startDate == '' || daysToAdd == '' || parseFloat(daysToAdd) == NaN) {
    $("#endDate").setValue('');
  }
  
  var end = moment(startDate).add(daysToAdd, 'days').format("YYYY-MM-DD");
  $("#endDate").setValue(end);
}

$("#startDate").setOnchange(function(newDate, oldDate) {
  calculateEndDate( newDate, $("#daysToAdd").getValue() );
})add days to date.json

$("#daysToAdd").setOnchange(function(newDays, oldDays) {
  calculateEndDate( $("#startDate").getValue(), newDays );
})
Where "startDate" and "endDate" are the IDs of datetime fields and "daysToAdd" is a text field. To see an example of this code, import the following DynaForm:
add days to date.json

Re: Add Days to date

Posted: Tue Jun 19, 2018 7:41 am
by Leenomeedo
Solved Thank you very much

Re: Add Days to date

Posted: Tue Jun 19, 2018 12:14 pm
by Leenomeedo
Sorry for interruption but its not working via php trigger while its working via java script will you kindly please help me to run the trigger via php?
amosbatto wrote: Mon Jun 18, 2018 8:48 pm If you want to do this in a trigger with PHP:
Code: Select all
@@endDate = date("Y-m-d", strtotime(@@startDate, @%daysToAdd));
Where "startDate" is the variable associated with a datetime field and "daysToAdd" is the variable associated with a text field in a previous DynaForm and "endDate" is the variable associated with a datetime in a future DynaFom.

If you want to do this with JavaScript in a DynaForm, then you can use the following code:
Code: Select all
function calculateEndDate(startDate, daysToAdd) {
  if (startDate == '' || daysToAdd == '' || parseFloat(daysToAdd) == NaN) {
    $("#endDate").setValue('');
  }
  
  var end = moment(startDate).add(daysToAdd, 'days').format("YYYY-MM-DD");
  $("#endDate").setValue(end);
}

$("#startDate").setOnchange(function(newDate, oldDate) {
  calculateEndDate( newDate, $("#daysToAdd").getValue() );
})add days to date.json

$("#daysToAdd").setOnchange(function(newDays, oldDays) {
  calculateEndDate( $("#startDate").getValue(), newDays );
})
Where "startDate" and "endDate" are the IDs of datetime fields and "daysToAdd" is a text field. To see an example of this code, import the following DynaForm:
add days to date.json

Re: Add Days to date

Posted: Tue Jun 19, 2018 11:27 pm
by amosbatto
Oh sorry, I see that the PHP code I gave you doesn't work. Try this:
Code: Select all
if (!empty(@@startDate)) {
   $sDate = @@startDate.' +'.@%numberDaysToAdd.' days';
   @@endDate = date("Y-m-d", strtotime($sDate));
}
Here is a sample process to try it:
(29.67 KiB) Downloaded 275 times

Re: Add Days to date

Posted: Wed Jun 20, 2018 5:47 am
by Leenomeedo
Hi
Thank You for your help and support I attached process the case what i want just its simple sample form of leave request content of number of days request,date of starting leave,last date before starting leave (requested date-1) , end Of leave(date of starting leave+number of days) and last filed is return back from leave (end of leave + 1).
Will you please in the same form guide me to add trigger in php to proceed the result before submit
Thank you

Re: Add Days to date

Posted: Wed Jun 20, 2018 9:37 pm
by amosbatto
Leenomeedo wrote:Thank You for your help and support I attached process the case what i want just its simple sample form of leave request content of number of days request,date of starting leave,last date before starting leave (requested date-1) , end Of leave(date of starting leave+number of days) and last filed is return back from leave (end of leave + 1).
Sorry, but I don't understand this sentence. Can you make it clearer what you want?

Re: Add Days to date

Posted: Thu Jun 21, 2018 2:55 am
by Leenomeedo
Thank you for your reply i wish this time I am clear find below the demonstration of my requirements:
The Form content of 5 fields as bellow:-

1- Number of leave days request (This field fill by the user manually example 10 )
2- Staring date of leave (This filed let user to select from calender the first day of leave example 10/06/2018)
3- last working day ( This filed represent the date of last working days calculated by step 2 - (1 day) to be calculated as 09/06/2018).
4- The day of end of leave ( this filed represent last date in leave calculated by step 2 + step 1 it will be appear 19/06/2018)
5- Return back to office (this filed represent as date of return day calculated by step 4 + (1 day ) to be appear 20/06/2018
* Kindly download attachment in my previous post
Please accept my appreciation

Re: Add Days to date

Posted: Fri Jun 22, 2018 9:20 am
by AdamSmith
thanks for the information :D