The CreatorCon Call for Content is officially open! Get started here.

Rest integration validating datetime and time zore

sushma9
Tera Contributor

Hi All,

 I have requirement to validate the dates .if the past date is selected  it should not accept the past date i need to show the error message and if the current date and future date is provide it should accept . I have tried with below code but i ma not able to do it. Please find the script and let me know if any modifications required.

 

Script :

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var respObj = {};
var timezone = request.queryParams.timezone.toString();
var inputdate = request.queryParams.date.toString();
var dataValidationErrors = [];
var today = new GlideDateTime(); // returns date time value as per system timezone
var message;
var answer;
if ((timezone.substring(4, 5) !== '+' && (timezone.substring(4, 5) !== '-'))) {
var input = timezone.substring(4, 7).toString(); // get hours part from timezone
var pm = timezone.indexOf('-');
var inputtime = timezone.slice(pm + 1, 7); // get hours part from timezone
var hrtomillisec = input * 3600000; //convert hours part of input timezone to milliseconds
var mn = timezone.indexOf(':');
var minutes = timezone.slice(mn + 1, 10); // get minutes part of timezone
var mintomilliseconds = minutes * 60 * 1000; // convert minutes part of the input timezone to milliseconds
var timeZoneOffSet = mintomilliseconds + hrtomillisec; // add milliseconds from hour and minutes to get final offset //
inputdate.setNumericValue(today.getNumericValue() + timeZoneOffSet);

} else {
var input = timezone.substring(4, 7).toString(); // get hours part from timezone
var pm = timezone.indexOf('-');
var inputtime = timezone.slice(pm + 1, 7); // get hours part from timezone
var hrtomillisec = input * 3600000; //convert hours part of input timezone to milliseconds
var mn = timezone.indexOf(':');
var minutes = timezone.slice(mn + 1, 10); // get minutes part of timezone
var mintomilliseconds = minutes * 60 * 1000; // convert minutes part of the input timezone to milliseconds
var timeZoneOffSet = mintomilliseconds + hrtomillisec; // add milliseconds from hour and minutes to get final offset //
today.setNumericValue(today.getNumericValue());
}


var targetDate = new GlideDateTime(today);
var endRequested = new GlideDateTime(inputdate);
var targetDatesplit = targetDate.toString().split(' ');
var datesplit = inputdate.toString().split(' ');
var targetDateFinal = targetDate.getDate();
var endRequestedFinal = endRequested.getDate();
if (endRequestedFinal.compareTo(targetDateFinal) == -1) {
dataValidationErrors.push("Provided date is a past date.please provide the future date");

} else if (endRequestedFinal.compareTo(targetDateFinal) == 0) {
message = "Time provided is valid";

} else {
message = "Time provided is valid";

}


if (dataValidationErrors.length > 0) {
//Validation failed, return Bad Request with error details
return new sn_ws_err.BadRequestError(dataValidationErrors.join(' | '));
}
respObj.message = message;
response.setBody(respObj);
//}

0 REPLIES 0