another date challenge Antoni Gual Posting Freak Posts: 1,407 Threads: 117 Joined: Dec 2002 12-19-2006, 02:07 AM Aga: I guess what you consider interesting is a description of the DayofWeek routine so here it goes. I don't know who invented it but it gave me a good headache to figure how does it work. Code:```FUNCTION DayofWeek (y, m, D)   '0=monday   DIM P, Q   IF m > 2 THEN     P = m - 3     Q = y   ELSE     P = m + 9     Q = y - 1   END IF DayofWeek = (D + 1 + Q + Q \ 4 - Q \ 100 + Q \ 400 + CINT(2.6 * P)) MOD 7 END FUNCTION``` It uses modular arithmetics to add offsets modulo 7 from a theoretical (gregorian) march 1th of the year zero. If that day existed it was a wednedsday. And it uses a shifted year (Q variable) starting at march 1. The monhts are also shifted, month 0 is march and month 11 is february. If you divide 365 by 7 you get a remainder of 1, so if you don't count leap years, every year advances a date by 1 position in the week. So we have a +Q in the formula. the +Q\4-Q\100+\400 part provide for the leap year's additional offsets. Then it comes the serious part, the reason why we use a shifted year. CINT(2.6*P) provides for the month of year offset. The effect of the fp operation plus rounding makes this factor add 3 days (31-28) for april (month 1), 5 days for may, 8 days for june... and so on, including the irregularity of august and september that require 3 days for each. February has been noved to the end of the year so its different behavior in leap years must not be considered in this step. Then the formula adds the Day of month (+D) and a 1 to center the things and make the result of 0 a monday . Hope it helps... Moneo: In my last post I was obviously not speaking about the same soccer game as you. In fact i found about the BarÃ§a- America in the net after re-reading yout post . I'm not very interested in soccer, you see... And for the explanation of the DayofWeek routine, my memory failed, we spoke about it at Pete's site, not at Foronet. Quote: Antoni Agamemnus Posting Freak Posts: 3,368 Threads: 195 Joined: Jan 2003 12-25-2006, 06:13 AM Yes, I guess that how I would go about it is figure out how many years since now were not leap years and then do MOD 7. Thanks for the explanation. I actually figured out that this can be done with FB's functions without resorting to formulas, but I was interested in a use for this in Javascript, actually: A function for a project of mine needs to get the next trading day (e.g.: Monday through Friday) X days from day N, year M: It would adjust the day if it was Sunday, Saturday, or another non-trading day. I can adjust the days easily enough, but getting the day of the week would have been difficult. Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war." Visit www.neobasic.net to see rubbish in all its finest. Antoni Gual Posting Freak Posts: 1,407 Threads: 117 Joined: Dec 2002 12-25-2006, 03:30 PM If your project requires to count n days from a day, just convert to julian dates. With JD you can get the day of the week simply by doing JD MOD 7. Antoni Moneo Posting Freak Posts: 1,956 Threads: 65 Joined: Jun 2003 12-26-2006, 01:00 AM Quote:...... A function for a project of mine needs to get the next trading day (e.g.: Monday through Friday)..... I think you're going to have to identify holidays, falling on Monday to Friday, which are not trading days for the location where the program is to be run. This a pain because you need to create a table or file of these holidays, and update the table every year. You could also try to program these holidays. Examples in the USA: Martin Luther King's birthday is celebrated on the third Monday in January. Thanksgiving is the fourth Thursday in November. If Good Friday is a holiday at your location, you will also need to compute the date of Easter. Banking laws in some countries inhibit banks from being closed for more than 4 days. So, if a Friday is a holiday, then Thursday and Monday cannot both be holidays. Trading days generally adhere to bank holidays. Computing all these holidays can get tedious. The holiday table or file is less elegant and perhaps prone to user error, but much simpler. EDIT: I think that The best solution would be to get the holiday table/file from the applicable stock exchange directly. Regards..... Moneo « Next Oldest | Next Newest »