Joomla 5 Notice

We are pleased to announce that as of January 29, 2024, all of our Joomla extensions are compatible with Joomla 5.

For all who are still updateing from Joomla 3 to Joomla 4: Joomla 4 Migration instructions are available here:

There is now a separate Documentation for Visforms for Joomla 4 and for Visforms for Joomla 5!

Forum

Visforms Subscription user can ask questions in our forum. Please log in with the relevant user first.
Everybody can access the forum for reading.

Please only ask 1 question per topic.

Important information for almost every question:
V1: Which Visforms version is running?
V2: Which Joomla version is running?
V3: Which PHP version is running?

Datetime Option

More
11 months 3 weeks ago #9907 by LittleCloud
Datetime Option was created by LittleCloud
Hi there,

I need for my users to select and set two datetime fields so I can determine the number of hours slept. I also need to use the datetime fields to average out the time they went to bed and obtain the average time they woke up. I can't find any options on the date field that would allow the selection of time.

(1) Does this need to be added to the frontend webassets section as javascript code?
(2) If so, how do I create a field to contain both date and time, or do I need to have them separate (one as a date field and the other as a number field with custom validation [regex] included)?
(3) If I do separate them, any idea as to how I can calculate both the date and time together to obtain the number of hours slept and average the time they went to bed or woke up?

Any help would be greatly appreciated.

Nelson

More
11 months 3 weeks ago #9908 by LittleCloud
Replied by LittleCloud on topic Datetime Option
Hi there, 

No need to reply, I figured it out.

// JS FUNCTION TO DETERMINE THE NUMBER OF SLEEP HOURS BETWEEN TWO DATES AND TIMES (COMBINED)
jQuery(document).ready(function() {
    console.log('FEWA script loaded');
    const enddateFieldID = '68'; // Visforms end date field ID of field list
    const endtimeFieldID = '69'; // Visforms end time (text) field ID of field list
    const startdateFieldID = '66'; // Visforms start date field ID of field list
    const starttimeFieldID = '67'; // Visforms start time (text) field ID of field list

    function calculateHours() {
        // Get values from input fields
        const startDateValue = jQuery(`#field${startdateFieldID}`).val();
        const startTimeValue = jQuery(`#field${starttimeFieldID}`).val();
        const endDateValue = jQuery(`#field${enddateFieldID}`).val();
        const endTimeValue = jQuery(`#field${endtimeFieldID}`).val();

        // Combine date and time strings
        const startDateTimeString = startDateValue + 'T' + startTimeValue;
        const endDateTimeString = endDateValue + 'T' + endTimeValue;

        // Create Date objects
        const startDate = new Date(startDateTimeString);
        const endDate = new Date(endDateTimeString);

        // Calculate time difference in milliseconds
        const timeDifference = endDate - startDate;

        // Convert milliseconds to hours
        const hoursDifference = timeDifference / (1000 * 60 * 60);

        // Display the result
        jQuery(`#field70`).val(hoursDifference);

        // If you want to log the result to console
        //console.log('Number of hours between the two dates:', hoursDifference);
    }

    // Call the function when any of the relevant fields change
    jQuery(`#field${startdateFieldID}, #field${starttimeFieldID}, #field${enddateFieldID}, #field${endtimeFieldID}`).on('change', calculateHours);
    
    // You can also call the function initially if needed
    // calculateHours();
});

Gotta love ChatGPT! LOL. 

Take care, Nelson
The following user(s) said Thank You: Administrator IV

More
11 months 2 weeks ago - 11 months 2 weeks ago #9910 by Administrator IV
Replied by Administrator IV on topic Datetime Option
Hello Nelson,

Thank you very much for sharing your great solution!
I think your idea with the additional text field for entering the time is great.
Your code is well structured and also very readable.

I plan to include more interesting code examples on the FEWA examples documentation page.
Examples for solving requirements with the Frontend Web Assets:
docs.joomla-4.visforms.vi-solutions.de/e...-frontend-web-assets

I will take the code examples from the answers in the forum.
If you don't mind, I would like to include your example too.
Is this o.k. for you?

Take care, Ingmar

:idea: I recommend you the new and up-to-date documentation for Joomla 4:
docs.joomla-5.visforms.vi-solutions.de/en/docs/
Most of this also applies retrospectively to Joomla 3.
Please only ask 1 question per topic :-).

:idea: Ich empfehle Dir die neue und aktuelle Dokumentation für Joomla 4:
docs.joomla-5.visforms.vi-solutions.de/docs/
Das meiste gilt rückwirkend auch für Joomla 3.
Bitte immer nur 1 Frage pro Thema stellen :-).
Last edit: 11 months 2 weeks ago by Administrator IV.

More
11 months 2 weeks ago #9911 by LittleCloud
Replied by LittleCloud on topic Datetime Option
Hi Ingmar,

What a great idea. If it will help someone else, by all means! I'm starting to see the power of Visforms. Great product. Thank you.

Nelson

More
11 months 2 weeks ago #9919 by Administrator IV
Replied by Administrator IV on topic Datetime Option
Nelson,

I have included this example in the documentation.

Record a time period with date fields and time fields:
docs.joomla-4.visforms.vi-solutions.de/e...elds-and-time-fields

There is an issue for developing the documentation:
Issue [VF-938] Docu: FEWA: Add examples

Thank you again for the contribution!

Kind regards, Ingmar

:idea: I recommend you the new and up-to-date documentation for Joomla 4:
docs.joomla-5.visforms.vi-solutions.de/en/docs/
Most of this also applies retrospectively to Joomla 3.
Please only ask 1 question per topic :-).

:idea: Ich empfehle Dir die neue und aktuelle Dokumentation für Joomla 4:
docs.joomla-5.visforms.vi-solutions.de/docs/
Das meiste gilt rückwirkend auch für Joomla 3.
Bitte immer nur 1 Frage pro Thema stellen :-).

Moderators: Administrator AVAdministrator IV
Powered by Kunena Forum