Check if date is at least 2 days greater than current date
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2024 06:43 AM
My script works for counting the days. but if I set the month to earlier it still returns true
the output is wrong as the month is set to January
var systemDays = new GlideDate();
var twoDaysTime = systemDays.getByFormat('dd/MM/yyyy');
twoDaysTime.addDays(2);
if(requestedCollectionDate > twoDaysTime) {
var dateMessage = "The date you have selected " + requestedCollectionDate + " is greater than " + twoDaysTime + " ";
}
2 REPLIES 2
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2024 06:49 AM
Hi,
Try this function, just pass the date you want to compare
function isStartDateValid(startDate) {
var currentDate = new GlideDate();
var startDateGDT = new GlideDateTime(startDate);
var currentDateGDT = new GlideDateTime(currentDate);
var duration = GlideDate.subtract(currentDateGDT, startDateGDT);
var days = duration.getDayPart();
if (days >= 2) {
return true;
}
return false;
}
-Anurag
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2024 06:55 AM
Hi @will_forster,
Using client script (onSubmit):
function onSubmit() {
var currentDate = new Date();
var dateField = g_form.getValue('your_date_field');
if (dateField) {
var fieldDate = new Date(dateField);
var futureDate = new Date();
futureDate.setDate(currentDate.getDate() + 2);
if (fieldDate <= futureDate) {
alert('The date must be at least 2 days greater than the current date.');
return false;
}
}
return true;
}
Thank you, please make helpful if you accept the solution.