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

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

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

    // Convert current date to AEST (UTC +10, accounting for DST)
    var currentDate = new Date();
    var localOffset = currentDate.getTimezoneOffset() * 60 * 1000; // Local timezone offset in milliseconds
    var aestOffset = 10 * 60 * 60 * 1000; // AEST offset in milliseconds
    var aestDate = new Date(currentDate.getTime() + localOffset + aestOffset);

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

    // Reset time components for accurate date-only comparison
    selectedDate.setHours(0, 0, 0, 0);
    aestDate.setHours(0, 0, 0, 0);
    futureDate.setHours(0, 0, 0, 0);

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

 

@nameisnani @Try with above Client script and ignore Script include and CS that was used before

 

Regards,

Sathish Kumar

@nameisnani 

We are here to provide sample script and syntax.

I believe with your required developer skill set you can enhance it, debug it and make it work.

We are not actually in your dev instance to debug the scripts so it would be very difficult to troubleshoot.

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

@nameisnani 

Hope you are doing good.

Did my reply answer your question?

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

@nameisnani 

Hope you are doing good.

Did my reply answer your question?

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

@nameisnani 

Hope you are doing good.

Did my reply answer your question?

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