• Resolved svd89

    (@svd89)


    Hello CodePeople,

    First of all, thank you so much for this wonderful plugin! I am having issues with translating hours to days. I’ve got the DATEDIFF set but I want to work with ’24 hours = 1 day. ‘1 day = 24 hours.’
    Or actually, 0-35 hours will be 1 day, 36-72h will be 2 days etc. and with a minimum of 1 day.
    Hope you can help me with this. Many thanks in advance and have a great day!

Viewing 15 replies - 1 through 15 (of 16 total)
  • Plugin Author CodePeople2

    (@codepeople2)

    Hello @svd89

    I assume you have the number field fieldname1 with the number of hours and want to transform it into X days Y hours.

    In this case, you can use the DECIMALTOTIME operation:

    DECIMALTOTIME(fieldname1, 'h', 'dd days and hh hours');

    Learn more about the Date/Times operations by reading the following section in the plugin documentation:

    https://cff.dwbooster.com/documentation#datetime-module

    Best regards.

    Thread Starter svd89

    (@svd89)

    Hello!

    Thank you so much for your reply. I have seen the documentation and the code (and got it working!) but that is not what I mean. I’ll try to explain from the start:

    We want customers to select two dates/times and the difference between the two dates will be visible. I also got that working. BUT! We want hours to be involved in the calculation. For example: I select today (04-12-2024) – 15:00 as a start and the end will be tomorrow, 05-12-2024 – 22:00. That is a total of 1 day and 7 hours. We want the ‘day-counter’ to stay at 1 day. Because 1 day means 24 hours. If the difference between (days and) hours will be 12hours or more, the day will be round upwards, less than 12 hours the day counter will be round downwards.
    I hope you know what I mean. I can explain myself better in Dutch (I’ve seen you CodePeople are Dutch-based 😀 ) so if needed, I’ll try it again in Dutch.

    Have a great day!

    Plugin Author CodePeople2

    (@codepeople2)

    Hello @svd89

    Yes, that’s possible.

    For example, assuming you have the date/time fields fieldname1 and fieldname2, you can insert a calculated field in the form and enter the following equation:


    (function(){
    let diff = DATEDIFF(fieldname1, fieldname2, 'mm/dd/yyyy hh:ii', 'd');
    return diff['days']+IF(diff['hours']<12, 0, 1);
    })()

    Best regards.

    Thread Starter svd89

    (@svd89)

    Yes, thank you! I would never have figured this out without your help.
    One more thing; the outcome has to have a minimum of 1. How can I implement that in your code?
    Hope to hear from you again. Many thanks in advance!

    Plugin Author CodePeople2

    (@codepeople2)

    Hello @svd89

    Use the MAX operation:

    (function(){
    let diff = DATEDIFF(fieldname1, fieldname2, 'mm/dd/yyyy hh:ii', 'd');
    return MAX(1, diff['days']+IF(diff['hours']<12, 0, 1));
    })()

    Best regards.

    Thread Starter svd89

    (@svd89)

    Wow YES! That did the trick! I was wondering.. why use MAX while I need a minimum?

    Again, many thanks. You are a lifesaver!

    All the best

    Plugin Author CodePeople2

    (@codepeople2)

    Hello @svd89

    If 1 is the minimum value accepted, the result must be the maximum value between 1 and the result of the mathematical.

    Best regards.

    Thread Starter svd89

    (@svd89)

    Ah, I see! Thank you so much for clarifying. I really appreciate it.
    Have a great day!

    Thread Starter svd89

    (@svd89)

    Hello!
    Here I am again. What if I want to include day or days in the code?
    So the outcome will be 1 DAY or 2+ DAYS?
    Hope to hear from you again. Many thanks in advance!

    Plugin Author CodePeople2

    (@codepeople2)

    Hello @svd89

    In this case, you can include an “IF” conditional operation in the equation:

    (function(){
    let diff = DATEDIFF(fieldname1, fieldname2, 'mm/dd/yyyy hh:ii', 'd');
    let result = MAX(1, diff['days']+IF(diff['hours']<12, 0, 1));
    return CONCATENATE(result, IF(result==1, ' DAY', ' DAYS'));
    })()

    Best regards.

    Thread Starter svd89

    (@svd89)

    Wow, this is amazing! Thank you very much! My wish is to display it as

    1
    DAY

    Instead of 1 day. So both are centered, with a <br>. Do I need to work with css here?

    Plugin Author CodePeople2

    (@codepeople2)

    Hello @svd89

    Calculated fields are input tags, and the input tags are single line controls and do not render HTML, so, you must use the calculated field as an auxiliary by display the result in another control, like an “HTML Content” field.

    Please be aware that our support services do not extend to the implementation of users’ projects. If you require personalized technical assistance, we encourage you to reach out to us directly through the plugin’s official website. Thank you for your understanding! Contact Us.

    Best regards.

    Thread Starter svd89

    (@svd89)

    Thank you so much for your reply and I understand completely. You have helped me very well. Thanks again!
    All the best and have a great day!

    Thread Starter svd89

    (@svd89)

    Hello @codepeople2 ,

    In addition of this topic I was wondering if it is also possible to add time within the ‘min. date’ field.
    Fieldname 2 has now a min. date of fieldname1, but I am trying to get the min. date of ‘fieldname 1 + 2 hours. ‘ Is this possible?

    Plugin Author CodePeople2

    (@codepeople2)

    Hello @svd89

    You cannot include mathematical operations in the date/time fields’ settings, but you can do it by using a calculated fields as an auxiliary.

    Assuming your date/time fields’ values have the format “mm/dd/yyyy hh:ii”, you can insert a calculated field to be used as an auxiliary (you can hide it by ticking a checkbox in its settings) and enter the equation:


    (function(){
    let d1 = DATEOBJ(fieldname1), d2 = DATEOBJ(fieldname2);
    d1.setHours(d1.getHours()+2);
    if (d2 < d1){
    getField(fieldname2|n).setVal(GETDATETIMESTRING(d1, 'mm/dd/yyyy hh:ii'));
    }
    })()

    If your date/time fields have a different date/time format, you must replace mm/dd/yyyy hh:ii in the equation with the corresponding one.

    Best regards.

Viewing 15 replies - 1 through 15 (of 16 total)

The topic ‘Hours to days’ is closed to new replies.