need configure date field based on logined user time zone
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2024 12:27 AM
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');
}
}
Please help me on the script.
Thanks ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2024 08:45 AM - edited 12-19-2024 08:54 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2024 08:50 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2024 08:56 AM
Updated in above post
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2024 08:59 AM - edited 12-19-2024 09:20 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2024 09:12 AM