How to compare 2 dates ?

Post Reply
beber005
Posts: 57
Joined: Mon Mar 03, 2014 2:18 pm
OLAP Product: Cognos
Version: 9.5.1
Excel Version: 2010

How to compare 2 dates ?

Post 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 :D
Alan Kirk
Site Admin
Posts: 6667
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: How to compare 2 dates ?

Post 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 :D
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.
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
declanr
MVP
Posts: 1831
Joined: Mon Dec 05, 2011 11:51 am
OLAP Product: Cognos TM1
Version: PA2.0 and most of the old ones
Excel Version: All of em
Location: Manchester, United Kingdom
Contact:

Re: How to compare 2 dates ?

Post 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.
Declan Rodger
Alan Kirk
Site Admin
Posts: 6667
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: How to compare 2 dates ?

Post 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.
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
beber005
Posts: 57
Joined: Mon Mar 03, 2014 2:18 pm
OLAP Product: Cognos
Version: 9.5.1
Excel Version: 2010

Re: How to compare 2 dates ?

Post 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 :D
Post Reply