Dwallace
22nd February 2003, 02:50
Is there any quick command to calc the days in a month based on the date? I know I can code it manually....
Paul P
22nd February 2003, 03:09
Dear Denise,
You mean you need something even shorter than mo.dy=sprintf$ (http://www.baanboard.com/programmers_manual_baanerp_help_functions_formatting_io_sprintf)("%D(%02d)",a.date) ?
Rgds,
Paul
Dwallace
22nd February 2003, 03:51
I mean # of days (28, 29, 30 or 31) not the 'day'of the month.
morpheus
22nd February 2003, 08:02
Denise,
You can use sprintf$(). Then use the CASE statement to find the number of days in the month, depending upon the month number. Also, find out the year, using the same function, to determine leap year, and hence the days.
Hope this helps.
mohan05
22nd February 2003, 09:11
HI
You will get the number of days in a month in following way
mon.days = sprintf$("%D(%02m)", date.num())
-Mohan
morpheus
22nd February 2003, 09:33
Originally posted by mohan05
HI
You will get the number of days in a month in following way
mon.days = sprintf$("%D(%02m)", date.num())
-Mohan
This would give the month number.
RobertB
22nd February 2003, 13:38
Here's another "knife-and-fork" approach (but it works on Baan 5 ERP) :D :
| How many days in February, 2000? - try the 15th of that month.....
message("Days = %d", days.in.month(date.to.utc(2000, 2, 15, 0, 0, 0)))
| Or something like this.....
message("Days = %d", days.in.month(tdsls400.odat))
function long days.in.month(domain tcdate i.date)
{
long year.num, month.num, day.num, hours, mins, secs
long ret, this.day, month.day
| For UTC dates.............
ret = utc.to.date(i.date, year.num, month.num, day.num, hours, mins, secs)
month.day = 0
while TRUE
month.day = month.day + 1
this.day = date.to.utc(year.num, month.num, month.day, hours, mins, secs)
if (this.day = -1) then | invalid date => overrun...
month.day = month.day - 1
break
else
endif
endwhile
return(month.day)
}HTH
dnnslbrwn
24th February 2003, 17:30
By fluke, I was reading a web site about shell programming last week and they had something like this as a sample solution...
month= (I am sure you can get this from above :^)
monthstr="312831303130313130313031"
daysinmonth = monthstr(month * 2 - 1; 2)
Cheers,
-Dennis
Paul P
25th February 2003, 03:18
Sweet solution!
RobertB
25th February 2003, 08:07
What about Leap Years, Dennis ? ;)
Paul P
25th February 2003, 09:01
Oh, well, I guess Dennis' solution was only 97.9166% accurate :)
RobertB
25th February 2003, 10:23
Well Paul, I hate to be an egg-head :D but, since leap years are determined according to the following rule: Every year that is exactly divisible by 4 is a leap year, except for years that are exactly divisible by 100; these centurial years are leap years only if they are exactly divisible by 400then Dennis' solution would only be around 75% accurate and not 97.9166%:p
p.cole
25th February 2003, 14:55
Untested, but should be ok.
function days.in.month(domain tcdate dte)
{
long days,month, year
num.to.date(dte, year, month, days)
if month = 12 then
month = 1
year = year + 1
else
month = month + 1
endif
num.to.date(date.to.num(year, month, 1) - 1, year, month, days)
return(days)
}
Ilansu
25th February 2003, 16:16
function days.in.month(ref domain tcpono num.of.days,
domain tcmonth month,domain tcyrno year)
{
long long_2
on case month
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:num.of.days=31
break
case 2: long_2=year\4
if long_2=0 then
num.of.days=29
else
num.of.days=28
endif
break
case 4:
case 6:
case 9:
case 11:num.of.days=30
break
endcase
}
dnnslbrwn
25th February 2003, 17:11
Well, I guess there are two solutions to the leap year issue,
1) you are practical and recognize that you code will not see the year 2100 and you add...
if year\4 = 0 then
monthstr="312931303130313130313031"
else
monthstr="312831303130313130313031"
endif
2) but on the other hand, you are a TRUE BELIEVER and you know in your heart of hearts that BAAN will be around to the end of time, then I guess you should use
if year\4=0 and not year\100=0 and year\400=0 then
monthstr="312931303130313130313031"
else
monthstr="312831303130313130313031"
endif
In reality, I think I would probably end up using the second because I _KNOW_ that BAAN will be around until the end of time and that it is the RIGHT solution.
Cheers,
-Dennis
RobertB
25th February 2003, 17:40
I _KNOW_ that BAAN will be around until the end of time and that it is the RIGHT solution.Yeehaw and amen to that!
Paul P
26th February 2003, 04:23
Dear RobertB,
Sorry, but the accuracy of Dennis' original solution was way, way higher than 75% you stated. 97.9166% is also not it, but is far closer (I didn't consider the rules about the century years)
Out of 400 years, there will be 99 leap years, right? That means 99 Februaries will have 29 days instead of 28 days. That means Dennis' original solution will be wrong for 99 Februaries (99 months) out of 400 years (4800 months). And that means that Dennis' original solution is 97.9375% accurate :) . So don't worry, RobertB, you were not being an egg head :)
But it's good that now we have even better solution than that :)
Rgds,
Paul
RobertB
26th February 2003, 08:05
Ouch! humble pie for breakfast - again!! But I was never any good at hard sums...:(
Paul P
26th February 2003, 08:12
But you were good at spotting the problem with the leap years first, RobertB :)
dnnslbrwn
26th February 2003, 16:50
Thanks for the defense Paul... I was feeling :( when I was only 75% right.
However, since I had to actually learn the rules for leap years again (funny how you have to do that about once ever 4 years) I did learn that it is every four years except for centuries which don't count except for centuries that divide by four that count again. :confused:
For three of those centuries there are 24 leap years each and on the fourth there is 25... so in 400 years there are 97 leap years. Don't know what that makes in % and it is WAY higher than 75% :D but it is unfortunately not quite 97.9375%
Cheers,
-Dennis
Paul P
27th February 2003, 03:22
Aaargh, d'oh on my part too. I'm not good at the rules of leap years :)
So your original solution is 97 Februaries (97 months) wrong out of 400 years (4800 months). And that means your original solution actually has even higher accuracy at 97.9792% ! :)
Rgds,
Paul
zardoz
27th February 2003, 11:37
A simpler way is to calculate the date.num of the first day of the next month and subtract 1, then extract the day number from num.to.date function:
function long days.in.month(long m, long y)
{
long d, dummy
m = m + 1
if m = 13 then
m = 1
y = y + 1
endif
num.to.date((date.to.num(y, m, 1) - 1), dummy, dummy, d)
return(d)
}
RobertB
27th February 2003, 14:37
A sweet solution and no hard-coded strings - which my grandpa always told me to steer clear of! And looking at your solution reminds me now that I built a date.diff() DLL function last year (not a leap year!), where the "day" part does exactly what you've done here.
I must be getting old....:(