- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2025 11:08 PM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2025 11:56 PM
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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2025 11:13 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2025 11:36 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2025 11:53 PM
Can you tell me how to write it specifically? It would be even better if there were examples.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2025 11:56 PM
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);
}
}