Discussion:
Build a date programatically and detect invalid dates
(too old to reply)
terra_frugum
2010-08-25 18:26:03 UTC
Permalink
Maybe this is trivial, but I simply cannot find an elegant solution.

I want to build a 'date' object programatically, that is, by specifying the
day, month and year numbers.
This can be accomplished by:

int d = 25;
int m = 8;
int y = 2010;
date myDate = str2date(strfmt("%1/%2/%3", d, m, y), 123);

My problem is that I want to find out the cases when d/m/y resolve to an
invalid date, such as 30/2/2010.

Unfortunately, str2date converts automatically the string "30/2/2010" to
Feb. 28, 2010!
I don't need the system to change the date to something valid; I simply want
it to tell me whether my input can create a valid date or not.

I even tried to use System.DateTime from .NET like this

try {
sysDt = new System.DateTime(2010, 2, 29);
myDate = sysDt;
print strfmt("%1", myDate);
} catch {
print "invalid";
}

which is almost what I want, but in addition I am saddled with an infolog
error message "Object 'CLRObject' could not be created".

Thanks!
terra_frugum
2010-08-25 18:37:03 UTC
Permalink
Eventually I found a way, but only using .NET. I'm still curious if there is
an approach to accomplish this purely within X++.

Anyway, a .NET solution can be:

str dateFormat = "31/4/2010";
System.DateTime sysDt;
boolean isValid = System.DateTime::TryParse(dateFormat, byref sysDt);
if (isValid) {
myDate = sysDt;
print strfmt("%1", myDate);
} else {
print dateFormat + " is invalid date";
}

Thanks.
unknown
2010-08-26 10:06:50 UTC
Permalink
Post by terra_frugum
Maybe this is trivial, but I simply cannot find an elegant solution.
I want to build a 'date' object programatically, that is, by specifying the
day, month and year numbers.
[...]
My problem is that I want to find out the cases when d/m/y resolve to an
invalid date, such as 30/2/2010.
Try that:

int d = 30;
int m = 2;
int y = 2010;

date myDate = mkdate(d, m, y);

if( dayofYr(myDate) != d
|| mthOfYr(myDate) != m
|| year(myDate) != y
)
{
myDate = dateNull(); //date not valid.
}

Hope that helps.

Regards
--
Michal
AXImprove | http://www.AXImprove.co.uk | Solutions for AX
performance and data storage
Loading...