We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

Hi @nameisnani ,

 

Why you are replying entire script provided by ankur just you have to place inside 

 

var TimeZone= Class.create(); TimeZone.prototype = Object.extendsObject(AbstractAjaxProcessor, {

//your script provided by ankur

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 '';
},

type: 'TimeZone' });



If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand

@Anand Kumar P 

 

Could you please provide complete script to avoid confusion 

Updated in above post 

Hi @nameisnani 

 

Kindly try with below script include and mark my answer as helpful if it works for you.

 

 

var TimeZone= Class.create(); TimeZone.prototype = Object.extendsObject(AbstractAjaxProcessor, {

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 '';
},

type: 'TimeZone' });

 

 

 

Alternatively try with below Client script and close the thread if it works!

 

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');
}
}

 

Regards ,

Sathish Kumar

@Anand Kumar P @SathishK1716321 

 

Result 

nameisnani_0-1734628334410.png

 

Not working as per the requiment