Working out Date range from a web form post

Working out Date range from a web form post

newbie here - ive created a webform where a client completes field to book a property.

In the form i have a check-in and check-out date and I wanted to include in the email trigger node the number of nights (check-in - check-out)

so far ive managed to create the workflow which takes the form data , created a contact in our cRM and then sends an email with the booking request. But i want to include the number of nights in the email but not sure how to do this?

thanks

Describe the problem/error/question

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
    2.13.4
  • Database (default: SQLite): NA
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): running in docker Hostinger VPS
  • Operating system:

Hi @solp Welcome!
In code node i think you can use the javascript function diffTo() , like this:
{{ $json.checkOutDate.toDateTime().diffTo($json.checkInDate.toDateTime(), 'days') }}
or with diff only:
{{ DateTime.fromISO($json.checkOutDate).diff(DateTime.fromISO($json.checkInDate), 'days').toObject().days }}
and then you can easily place this expression directly in your HTML mail, or more simpler way that to use a set field node and then calculate it before sending it on mail and then just directly pass that, a code node can easily handle that.

Thanks for the welcome @Anshul_Namdev

re your suggestion, sorry this is all still new to me. Would the below be ok (taking values from the Previous Node (form)?

{{ $(‘Direct Booking Enquiry’).item.json[“Check In Date”] }}DateTime.diff {{ $(‘Direct Booking Enquiry’).item.json[“Check Out Date”], ‘days’).toObject().days }}

Thanks

Hi Anshul
ok i did the solution using:

{{ Math.round((new Date($(‘Direct Booking Enquiry’).item.json[‘Check Out Date’]) - new Date($(‘Direct Booking Enquiry’).item.json[‘Check In Date’])) / (1000 * 60 * 60 * 24)) }}

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.