Time Zone Onload Script

cpinedatx94
Tera Expert

Im attempting to make a Client script which takes a current time field and a time zone choice field and than auto calculates thats to Eastern time and plugs it into a field for me. This is what it currently looks like but its not wanting to work

Could i please get some assistance. Thanks.

function onChange() {
    // Get the inputted time and time zone values
    var inputTime = g_form.getValue('u_ssa_install_time');
    var inputTimeZone = g_form.getValue('u_time_zone_kiosk');
    var hour = parseInt(inputTime.slice(0, 2)); // Parse the hour to an integer
    var timeOfDay = inputTime.slice(inputTime.length - 2); // Get AM or PM
    var timeOffset = 0; // Initialize time offset

    // Check if both input fields have values
    if (hour && inputTimeZone) {
        // Assign time offset based on selected time zone
        if (inputTimeZone == 'Atlantic') {
            timeOffset = 1;
        } else if (inputTimeZone == 'Eastern') {
            timeOffset = 0;
        } else if (inputTimeZone == 'Central') {
            timeOffset = -1;
        } else if (inputTimeZone == 'Mountain') {
            timeOffset = -2;
        } else if (inputTimeZone == 'Pacific') {
            timeOffset = -3;
        } else if (inputTimeZone == 'Alaska') {
            timeOffset = -4;
        } else if (inputTimeZone == 'Hawaii') {
            timeOffset = -5;
        }

        // Calculate Eastern Time
        var easternHour = hour + timeOffset;

        // Adjust hour and time of day if needed
        if (easternHour >= 12) {
            timeOfDay = 'PM';
            if (easternHour > 12) {
                easternHour -= 12;
            }
        } else if (easternHour < 0) {
            timeOfDay = 'AM';
            easternHour += 12;
        }

        // Set the value of the fields to display the calculated Eastern Time
        g_form.setValue('u_eastern_time', easternHour.toString().padStart(2, '0') + timeOfDay);
    }
}

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@cpinedatx94 Please try the following script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    //alert(newValue);
    //Type appropriate comment here, and begin script below
    // Get the inputted time and time zone values
    var inputTime = g_form.getValue('u_ssa_install_time');
    var inputTimeZone = g_form.getValue('u_time_zone_kiosk');
    var hour = parseInt(inputTime.slice(0, 2)); // Parse the hour to an integer
    var timeOfDay = inputTime.slice(inputTime.length - 2); // Get AM or PM
    var timeOffset = 0; // Initialize time offset
    alert(timeOffset);
    // Check if both input fields have values
    if (hour && inputTimeZone) {
        // Assign time offset based on selected time zone
        if (inputTimeZone == "atlantic") {
            timeOffset = 1;
        } else if (inputTimeZone == "eastern") {
            timeOffset = 0;
        } else if (inputTimeZone == "central") {
            timeOffset = -1;
        } else if (inputTimeZone == "mountain") {
            timeOffset = -2;
        } else if (inputTimeZone == "pacific") {
            timeOffset = -3;
        } else if (inputTimeZone == "alaska") {
            timeOffset = -4;
        } else if (inputTimeZone == "hawaii") {
            timeOffset = -5;
        }
        // Calculate Eastern Time
        var easternHour = hour + timeOffset;
        alert("\n" + hour + " " + easternHour);
        // Adjust hour and time of day if needed
        if (easternHour >= 12) {
            timeOfDay = "PM";
            if (easternHour > 12) {
                easternHour -= 12;
            }
        } else if (easternHour < 0) {
            timeOfDay = "AM";
            easternHour += 12;
        }
        alert("test" + easternHour.toString().padStart("00:00:00", '0') + timeOfDay);
        // Set the value of the fields to display the calculated Eastern Time
        //g_form.setValue('u_eastern_time', easternHour.toString().padStart(2, '0') + timeOfDay);
        //g_form.setValue('u_eastern_time', "11:00:00 AM");
        if (easternHour.toString().length == 1) {
            easternHour = '0' + easternHour;
        }
        var finalTime = easternHour.toString() + ':00:00 ' + timeOfDay;
        alert('final ' + finalTime);
        g_form.setValue('u_eastern_time', finalTime);
    }
}

View solution in original post

5 REPLIES 5

BalaG
Kilo Sage

Hi @cpinedatx94  please share the input values (from input fields) and the error you are receiving.

 

--

Bala G

Meloper
Kilo Sage

Hi, what Part is not working?
To get the Values and add the Hours or to Set the Value?
Do you get a Message in the Browser Consol?

pGaIFmk

 

I need that "Local Install Time" + "Timezone" = Correct Eastern Time.

Sandeep Rajput
Tera Patron
Tera Patron

@cpinedatx94 Please try the following script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    //alert(newValue);
    //Type appropriate comment here, and begin script below
    // Get the inputted time and time zone values
    var inputTime = g_form.getValue('u_ssa_install_time');
    var inputTimeZone = g_form.getValue('u_time_zone_kiosk');
    var hour = parseInt(inputTime.slice(0, 2)); // Parse the hour to an integer
    var timeOfDay = inputTime.slice(inputTime.length - 2); // Get AM or PM
    var timeOffset = 0; // Initialize time offset
    alert(timeOffset);
    // Check if both input fields have values
    if (hour && inputTimeZone) {
        // Assign time offset based on selected time zone
        if (inputTimeZone == "atlantic") {
            timeOffset = 1;
        } else if (inputTimeZone == "eastern") {
            timeOffset = 0;
        } else if (inputTimeZone == "central") {
            timeOffset = -1;
        } else if (inputTimeZone == "mountain") {
            timeOffset = -2;
        } else if (inputTimeZone == "pacific") {
            timeOffset = -3;
        } else if (inputTimeZone == "alaska") {
            timeOffset = -4;
        } else if (inputTimeZone == "hawaii") {
            timeOffset = -5;
        }
        // Calculate Eastern Time
        var easternHour = hour + timeOffset;
        alert("\n" + hour + " " + easternHour);
        // Adjust hour and time of day if needed
        if (easternHour >= 12) {
            timeOfDay = "PM";
            if (easternHour > 12) {
                easternHour -= 12;
            }
        } else if (easternHour < 0) {
            timeOfDay = "AM";
            easternHour += 12;
        }
        alert("test" + easternHour.toString().padStart("00:00:00", '0') + timeOfDay);
        // Set the value of the fields to display the calculated Eastern Time
        //g_form.setValue('u_eastern_time', easternHour.toString().padStart(2, '0') + timeOfDay);
        //g_form.setValue('u_eastern_time', "11:00:00 AM");
        if (easternHour.toString().length == 1) {
            easternHour = '0' + easternHour;
        }
        var finalTime = easternHour.toString() + ':00:00 ' + timeOfDay;
        alert('final ' + finalTime);
        g_form.setValue('u_eastern_time', finalTime);
    }
}