1
0
-1
I am developing a form and I want to set some logic so that the The Event Finish Date is not prior to the Event Start Date.
I have 2 date fields and then Time fields following.
Can you show me how to apply this to the Finish Date?
    CommentAdd your comment...

    1 answer

    1.  
      1
      0
      -1
      1. Matthew van Bockel
        One way to do this via script would be to:

        1. Convert the date & time field values to an ISO format: yyyy-MM-ddTHH:mi:ss

        2. Parse the ISO dateTime strings into Date objects and check if one is before (less than) the other


        Here's one example of doing this via script:

        // split the time value into hours & minutes and convert to 24hr time string:
        var time1 = {startTime}.split(":");
        var hour1 = parseInt(time1[0]);
        var min1 = parseInt(time1[1]);
        if('PM'==={startAMPM} && hour1 < 12) { hour1 = hour1+12; }
        if('AM'==={startAMPM} && hour1 == 12) { hour1 = hour1-12; }
        // create an ISO dateTime string value:
        var timestamp1 = {startDate}+"T"+(hour1<10?"0"+hour1:hour1)+":"+(min1<10?"0"+min1:min1)+":00";

        // split the time value into hours & minutes and convert to 24hr time string:
        var time2 = {endTime}.split(":");
        var hour2 = parseInt(time2[0]);
        var min2 = parseInt(time2[1]);
        if('PM'==={endAMPM} && hour2 < 12) { hour2 = hour2+12; }
        if('AM'==={endAMPM} && hour2 == 12) { hour2 = hour2-12; }
        // create an ISO dateTime string value:
        var timestamp2 = {endDate}+"T"+(hour2<10?"0"+hour2:hour2)+":"+(min2<10?"0"+min2:min2)+":00";

        // here's a sample parse and compare output:
        sfc.showAlert(
        "Timestamp1: "+timestamp1+"\n"+
        "Timestamp2: "+timestamp2+"\n"+
        "Timestamp1 < Timestamp2 ? : "+(Date.parse(timestamp1) < Date.parse(timestamp2))
        );

        Note: the above script assumes that:

        • All referenced fields are mandatory (cannot be blank)

        • The Time text fields use regular expression validation to enforce "nn:nn" values: ([0-9]){1,2}:([0-9]){2}



      CommentAdd your comment...