How to modify Java script based on time zone?

shiz
Tera Contributor
I have the following Java scientist code:
function onLoad() {
    //Type appropriate comment here, and begin script below
    if (g_form.isNewRecord()) {
        var today = new Date();
        var day = today.getDay();
        // Calculate the Monday of the current week
        var monday = new Date(today.setDate(today.getDate() - (day === 0 ? 6 : day - 1)));
       
        g_form.setValue('u_week_starts_on', monday.toISOString().split('T')[0]);
        // set field is readonly
        g_form.setReadOnly('u_week_starts_on', false);  
        g_form.setReadOnly('u_assigned_to', false);  
    }else{
        // set field is readonly
        g_form.setReadOnly('u_week_starts_on', true);  
        g_form.setReadOnly('u_assigned_to', true);  
    }
Now I have discovered this issue, when I set the time zone to the US location in Tokyo,
Due to the time difference, the value of 'Monday' obtained in the code is not the Monday of the current week, but the Sunday of the previous week.

I want to achieve that no matter where the time zone is switched globally, the value of 'Monday' obtained in the code will always be Monday. How can I modify the JavaScript in this way?
 
 
1 ACCEPTED SOLUTION

@shiz 

Here’s how you can modify your code to always calculate Monday based on UTC:

function onLoad() {
if (g_form.isNewRecord()) {
// Get current date in UTC
var now = new Date();
var utcDay = now.getUTCDay(); // Sunday = 0, Monday = 1, ..., Saturday = 6

// Calculate the UTC date of Monday of the current week
var utcMonday = new Date(Date.UTC(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate() - (utcDay === 0 ? 6 : utcDay - 1)
));

// Format as YYYY-MM-DD
var formattedMonday = utcMonday.toISOString().split('T')[0];

g_form.setValue('u_week_starts_on', formattedMonday);
g_form.setReadOnly('u_week_starts_on', false);
g_form.setReadOnly('u_assigned_to', false);
} else {
g_form.setReadOnly('u_week_starts_on', true);
g_form.setReadOnly('u_assigned_to', true);
}
}

If I was able to help you with your case, please click the Thumb Icon and mark as Correct.

View solution in original post

9 REPLIES 9

Another additional issue is that I want to check the Monday settings in the business role, but using the following code seems to yield an error. Do modifications also need to be made in the business role? How can you help me? Thank you so much.
 
(function executeRule(current, previous /*null when async*/) {

    // Add your code here
    var weekStartOn = new GlideDateTime(current.u_week_starts_on);
    var day = weekStartOn.getDayOfWeekLocalTime();
    if (day != '1') {
        //Check if the week start on is Monday
        //gs.addErrorMessage('Week start day has been set to Monday in the time sheet policy Default time sheet policy. You must choose a date that is a Monday.'); // Change the message as per your requirement
        gs.addErrorMessage(gs.getMessage('check_week_start_on'));
        current.setAbortAction(true);
    }

@shiz remove the quotes for day! = 1

 

function executeRule(current, previous /*null when async*/) {

var weekStartOn = new GlideDateTime(current.u_week_starts_on);
var day = weekStartOn.getDayOfWeekLocalTime();

// Check if the selected day is NOT Monday (Monday = 1)
if (day != 1) {
gs.addErrorMessage(gs.getMessage('check_week_start_on'));
current.setAbortAction(true);
}
}

If I was able to help you with your case, please click the Thumb Icon and mark as Correct.

function onLoad() {
if (g_form.isNewRecord()) {
var nowDate = new Date();

var utcDay = now.getUTCDay();

// Calculate the UTC date of Monday of the current week
var utcMonday = new Date(Date.UTC(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate() - (utcDay === 0 ? 6 : utcDay - 1)
));

g_form.setValue('u_week_starts_on', utcMonday.toISOString().split('T')[0]);

// Make fields editable
g_form.setReadOnly('u_week_starts_on', false);
g_form.setReadOnly('u_assigned_to', false);
} else {
// Make fields read-only for existing records
g_form.setReadOnly('u_week_starts_on', true);
g_form.setReadOnly('u_assigned_to', true);
}
}

 

Please mark it as helpful.

Another additional issue is that I want to check the Monday settings in the business role, but using the following code seems to yield an error. Do modifications also need to be made in the business role? How can you help me? Thank you so much.
 
(function executeRule(current, previous /*null when async*/) {

 

    // Add your code here
    var weekStartOn = new GlideDateTime(current.u_week_starts_on);
    var day = weekStartOn.getDayOfWeekLocalTime();
    if (day != '1') {
        //Check if the week start on is Monday
        //gs.addErrorMessage('Week start day has been set to Monday in the time sheet policy Default time sheet policy. You must choose a date that is a Monday.'); // Change the message as per your requirement
        gs.addErrorMessage(gs.getMessage('check_week_start_on'));
        current.setAbortAction(true);
    }

Ankur Bawiskar
Tera Patron
Tera Patron

@shiz 

what's your business requirement?

what are you trying to achieve?

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