Page 1 of 1
How to compare 2 dates ?
Posted: Sat Apr 26, 2014 12:53 pm
by beber005
Hi every one
I mess with dates. In fact, I do a test on them to tell if A is greater than B, then do it otherwise do that and so on.
That's what I currently write code like:
Code: Select all
# pMonth and pYear are my input parameters
vYM = '01-' | pMonth |'-' | pYear
vNewDate = DAYNO(vYM)
# vPastDate is an other date as same format as vNewdate
If(vNewdate > vPastDate);
# do that
ENdIf;
Unfortunately it does not work in all cases. For example, when I compare the dates with the date 01-02-2011 04-05-2005 he told me that the date 2005 is> 2011. Yet this is totally false. Where can it come? Should we add a calculation on the dates for comparison?
Thanks a lot for your help

Re: How to compare 2 dates ?
Posted: Sat Apr 26, 2014 7:14 pm
by Alan Kirk
beber005 wrote:Hi every one
I mess with dates. In fact, I do a test on them to tell if A is greater than B, then do it otherwise do that and so on.
That's what I currently write code like:
Code: Select all
# pMonth and pYear are my input parameters
vYM = '01-' | pMonth |'-' | pYear
vNewDate = DAYNO(vYM)
# vPastDate is an other date as same format as vNewdate
If(vNewdate > vPastDate);
# do that
ENdIf;
Unfortunately it does not work in all cases. For example, when I compare the dates with the date 01-02-2011 04-05-2005 he told me that the date 2005 is> 2011. Yet this is totally false. Where can it come? Should we add a calculation on the dates for comparison?
Thanks a lot for your help

Ah. I see what your problem is. You're on the right track, but you're feeding the values into the DayNo function in the wrong order. You have to feed them as Year-Month-Day not Day-Month-Year. I suggest referring to
this thread for an overview of working with dates and times.
Re: How to compare 2 dates ?
Posted: Sat Apr 26, 2014 8:50 pm
by declanr
And since you have to put it in the yyyymmdd format, if all you want to do is find out which one is more recent/older you can just convert them to numbers using stringtonumber()... same number of steps but just a different method.
e.g.
20130101 > 20120101
20130201 > 20130101
20130205 > 20130201
That way even as numbers they are also still recognisable to the user whereas serial numbers tend to not be.
Re: How to compare 2 dates ?
Posted: Sat Apr 26, 2014 9:47 pm
by Alan Kirk
Good suggestion from Declan, but just be careful that your year parameter is always 4 digits if you go down that path. Since it's a parameter (or appears to be) you may not have any control over that at runtime. The advantage of DayNo is that for the range 1 Jan 1960 to 31 Dec 2059 it can take either a 2 character year or a 4 character year and you'll get the correct result.
Re: How to compare 2 dates ?
Posted: Mon Apr 28, 2014 5:59 am
by beber005
Thank's alan and declanr for your help. Alan actually you were right. I do not think that the order be able to change all that. And now it works perfectly
