Stupid problems with date type

For questions and postings not covered by the other forums
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Stupid problems with date type

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:17 pm

by pillick >> Thu, 5 Dec 2002 2:24:42 GMT

Hey,

Im having alot of problems with something I thought would be very easy.

One of my funtions takes a date and returns the date exactly one month before it. The algorithm works like this:

if date.month > 1 then
prevDate.setDateYearAbsolute(date.day, date.month - 1, date.year) ; else
prevDate.setDateYearAbsolute(date.day, 12, date.year - 1) ;
endif ;

I cant see any reason why such a simple function would give the wrong answer, but somehow it does.

Here is an example of it going wrong:

----> 30 May 2001
----> 30 April 2001
----> 30 March 2001
----> *invalid*

Anyone have any idea whats going on??

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Stupid problems with date type

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:17 pm

by JensRichnow >> Thu, 5 Dec 2002 2:41:22 GMT

Could it be that you are trying to set to a non-existing date, e.g., 30/02/2002 in which case the date will be invalid.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Stupid problems with date type

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:17 pm

by allistar >> Thu, 5 Dec 2002 5:47:15 GMT
Could it be that you are trying to set to a non-existing date, e.g., 30/02/2002 in which case the date will be invalid.

I would say that that is the probme. 30/2/2002 isn a valid date. It should return 28/2/2002.

We have come across problems with this before. We want it to behave like this:

30/5/02
30/4/02
30/3/02
28/2/20
30/1/02
30/12/01

but instead we get:

30/5/02
30/4/02
30/3/02
28/2/20
28/1/02
28/12/01

The problem is we are simply removing 1 month off the month before. The solution is to keep using the starting date (30/5/02) and remove 1 month off it, then 2, 3, 4, 5, 6... and you would get the desired result.

Depending on the problem set you don't necessarily always know the original date, which makes this impossible.

Allistar.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Stupid problems with date type

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:17 pm

by JensRichnow >> Thu, 5 Dec 2002 2:45:38 GMT

Along the same line of my other post. The condition works fine if you use logical dates. Trying to force non-existing dates (such as 29/02/2001 or 30/02/2001) will result in invalid dates. There, the conditional statement should take these cases into consideration.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Stupid problems with date type

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:17 pm

by JensRichnow >> Thu, 5 Dec 2002 2:49:17 GMT

I just remembered that we had a similar problem but with adding a month to the date. Have a look at FAQ 10831 which gives an example how to add a month or year to a date. You should be able to use this example to 'reverse' it.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Stupid problems with date type

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:17 pm

by pillick >> Thu, 5 Dec 2002 3:55:11 GMT

cheers.

I didnt use the code in the faq, but just wrote my own daysInMonth function and used that. seems to work well.


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest