need configure date field based on logined user time zone

nameisnani
Mega Sage

Hi Team , 

 

can any one please help me on this requirement? 

 

For a particular catalog item, we have a date field ' expiry_date'

 

This date field should allow date before 6months only .  for that i have written onchange cilent script . which is working fine .

 

Now , the problem with the timezone of the users .

 

So therefore early this morning, I could only select 15th June 25, but then after 11am I can select 16th June. If possible we should make it AEDT to match Sydney time to avoid any questions from users .

 

can anyone please provide any other script so that it has to work in AEDT timezone .

 

cilent script or script include .

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    // Parse the selected date
    var gdt = new Date(newValue);

    // Convert to AEST time zone (UTC +10)
    var currentDate = new Date();
    alert('Get Time' + currentDate.getTime());
    var offset = 10 * 60 * 60 * 1000; // AEST offset in milliseconds
    currentDate = new Date(currentDate.getTime() + currentDate.getTimezoneOffset() * 60 * 1000 + offset);

    // Get the date 6 months from now in AEST
    var futureDate = new Date(currentDate.getTime());
    futureDate.setMonth(futureDate.getMonth() + 6);

    // Check if the selected date is in the past or beyond 6 months
    if (gdt < currentDate) {
        alert("You cannot select a past date.");
        g_form.clearValue('expiry_date');
    } else if (gdt > futureDate) {
        alert("You cannot select a date more than 6 months into the future.");
        g_form.clearValue('expiry_date');
    }
}

 

 

nameisnani_0-1734596537619.png

 

 

Please help me on the script.

Thanks ... 

14 REPLIES 14

Ankur Bawiskar
Tera Patron
Tera Patron

@nameisnani 

why not allow the script to check the time based on logged in user's timezone?

in that way it will work with all the users with different timezone

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

coould you please please provide me the script 

@nameisnani 

use GlideAjax for this

ValidateDate: function() {

    var selectedDate = new GlideDateTime(this.getParameter('sysparm_date'));

    var currentDateTime = new GlideDateTime();
    currentDateTime.setDisplayValue(gs.nowDateTime());

    if (selectedDate.before(currentDateTime))
        return 'You cannot select a past date.';
    else
        currentDateTime.addMonthsUTC(6);

    if (selected.after(futureDate))
        return 'You cannot select a date more than 6 months into the future.';
    else
        return '';
},

Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    if (oldValue != newValue) {
        var ga = new GlideAjax('DateValidation');
        ga.addParam('sysparm_name', 'validateDate');
        ga.addParam('sysparm_date', newValue);
        ga.getXMLAnswer(function(response) {
            var answer = response.responseXML.documentElement.getAttribute('answer');
            if (answer != '') {
                g_form.showFieldMsg('expiry_date', answer, 'error');
                g_form.clearValue('expiry_date');
            } else {
                g_form.hideFieldMsg('expiry_date', 'error');
            }
        });
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

 

nameisnani_0-1734626221430.png

 

 

What was the mistake here , could you please provide me the correct script .