- 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-04-2025 12:53 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2025 01:38 AM
@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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2025 12:08 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2025 12:57 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2025 12:13 AM
what's your business requirement?
what are you trying to achieve?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader