User enters something into text area on dialog (vSubSearch).
Program determines if it is a date; if so, performs a search of the likely (date) fields. Otherwise, performs a “contains” search.
The code –
Tests for date format using a global method.
Parses with mod_date.js, then utils.dateFormat to get target_d.
Performs search.
This seems overly complicated. Do I need the “#” and “|”-format when performing the search? In 4D I could test the validity of a date string with one line of code. Am I overlooking something?
Give what you have as a initialization parameter to a new Date() and it convert it into a date if possible. First convert the “-” to “/” though or set locale so that it matches. Then all you have to do is test to see if the date is valid.
var s = "2012/01/01";
try {
var d = new Date(s);
}
catch(e) {
application.output(e.message);
}
I tried enhancing your method so that it could tolerate a variety of formats. However, I ran into difficulty because the utils.dateFormat() will accept “6/12/2012” as being in the format “MM/dd/yyyy”. Is there a way around this?
Thanks,
Don
function UTIL_convertDateString(dateString, dateFormat) {
var testFormat = "";
var stringResult = "";
if((dateString) && (dateFormat)) {
var dateResult = utils.dateFormat(dateString,dateFormat);
if(!dateResult) { // it didn't convert using the specified format
testFormat = "MM/dd/yyyy"
dateResult = utils.dateFormat(dateString,testFormat);
}
if(!dateResult) {
testFormat = "MM-dd-yyyy"
dateResult = utils.dateFormat(dateString,testFormat);
}
if(!dateResult) {
testFormat = "MM.dd.yyyy"
dateResult = utils.dateFormat(dateString,testFormat);
}if(!dateResult) {
testFormat = "yyyy-MM-dd"
dateResult = utils.dateFormat(dateString,testFormat);
}
if(!dateResult) {
testFormat = "yyyy.mm.dd"
dateResult = utils.dateFormat(dateString,testFormat);
}if(!dateResult) { // it didn't convert using the specified format
testFormat = "yyyyMMdd"
dateResult = utils.dateFormat(dateString,testFormat);
}
if(dateResult) {
if(testFormat) {
stringResult = utils.dateFormat(dateResult,testFormat);
} else {
stringResult = utils.dateFormat(dateResult,dateFormat);
}
if (stringResult === dateString) {
return dateResult;
}
}
}
return null;
}
Hi Don,
I suppose it’s a good thing that it does accept 6/12/2012, because it’s a valid date, if it wasn’t doing this you would have to test for d/MM/yyyy and dd/M/yyyy for each of your formats.
I must say that I never ran into a case where this was an issue, so I never tried to find a better solution.
I suppose a Regexp would be the answer in that case…
Maybe I’m missing something, but could you not use a masked format in your text field in order to force the user to use a specific format? Then you could try mapping the text to a date object via the dateFormat (or dateParse in 6.1 I guess)
utils.dateFormat(vSubSearch, 'yourformat')
``` I seem to remember an error is thrown if the text cannot be parsed...
It’s a “smart” search box, that attempts to discern what the user is looking for (see attached). I use this in various locations to allow users to get to a particular record quickly.