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

Omkar Kumbhar
Mega Sage
Mega Sage

Hello @shiz ,

To ensure that calculated "Monday" is always consistent globally- regardless of the user's time zone- you should base your calculation on UTC time and not local time

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

 

Replace new Date() with Date.UTC() and use getUTCDay instead of getDay()

 

Best regards,

Omkar

 

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

VijayKumarDodde
Tera Contributor

Use getUTC*() methods instead of get*() methods to avoid time zone issues.

 

This guarantees that Monday is calculated consistently, no matter the user's location.

Can you tell me how to write it specifically? It would be even better if there were examples.

@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.