[comp.sys.ibm.pc] I need a date :) please help

mead@uxh.cso.uiuc.edu (04/07/90)

Sorry if you caught this in another group (but then why did you look at
this one?).

I am writing a simulation in which the user will get (simulated)
"memos".  These memos will have embedded symbols which my code shall
have to expand into appropriate dates (eg, @d3 might get expanded to
the string, 'Tuesday April 3, 1990' - the date three days before I'm
writing this note.  Tomorrow, it would be expanded to 'Wednesday April
4, 1990', ...).

QUERY:  Does anyone know of *any* source for an intelligent algorythm
for getting the day ('Monday, Tuesday, ...') from the date (4/3/90).
My PC does it like lightning each time I issue the 'DATE' command, and
if IBM can do it, anyone can - right? :)  Textbooks or whatever are
fine, but please don't direct me to existing software unless it's PD
source code (in BASIC, C, Pascal, or FORTRAN).  Thanks.

-alan mead : mead@uxh.cso.uiuc.edu

mouse@vaxb.acs.unt.edu (Dhanapong Saengrussamee, University of North Texas) (04/07/90)

In article <19500060@uxh.cso.uiuc.edu>, mead@uxh.cso.uiuc.edu writes:
> QUERY:  Does anyone know of *any* source for an intelligent algorythm
> for getting the day ('Monday, Tuesday, ...') from the date (4/3/90).
> My PC does it like lightning each time I issue the 'DATE' command, and
> if IBM can do it, anyone can - right? :)  Textbooks or whatever are
> fine, but please don't direct me to existing software unless it's PD
> source code (in BASIC, C, Pascal, or FORTRAN).  Thanks.
> 
> -alan mead : mead@uxh.cso.uiuc.edu

I assume that the program you try to write is for PC, right?

DOS interrupt $21 function $2A is what you need.
  Call with AH = $2A
  Return:  CX = year (1980 through 2099--don't know what will happen after
                      the year 2099 8^))
           DH = month (1 through 12)
           DL = day (1 through 31)
           AL = day of week (0 = Sunday, 1 = Monday)

Hope this help,
Mouse .o()~
-----------------------------------+------------------------------------------
Dhanapong "Mouse" Saengrussamee    | AppleLink : U1364 | BITNET : mouse@untvax
Technical Support, CECS Dept.,     | BIX : d.mouse.s | CompuServe : 71301,1516
University of North Texas,         | GENie : d.saengrussa | IP : 129.120.1.4
PO Box 5155, Denton, TX 76203-5155 | Internet : mouse@vaxb.acs.unt.edu
voice (871) 565-4379               | SPAN : utspan::utandx::ntvaxb::mouse
fax   (817) 565-4425               | THENet : ntvaxb::mouse
------------------- UUCP : {...!uunet!convex!iex}!ntvax!vaxb.acs.unt.edu!mouse

scjones@sdrc.UUCP (Larry Jones) (04/08/90)

In article <19500060@uxh.cso.uiuc.edu>, mead@uxh.cso.uiuc.edu writes:
> QUERY:  Does anyone know of *any* source for an intelligent algorythm
> for getting the day ('Monday, Tuesday, ...') from the date (4/3/90).
> My PC does it like lightning each time I issue the 'DATE' command, and
> if IBM can do it, anyone can - right? :)  Textbooks or whatever are
> fine, but please don't direct me to existing software unless it's PD
> source code (in BASIC, C, Pascal, or FORTRAN).  Thanks.

The traditional algorithm is (in C):

	weekday = (year + year/4 + startday[month-1] + day) % 7;
	if (year%4 == 0  &&  month < 3) --weekday;

year is either the full year or the last two digits, month is the
month (1 - 12), and day is the day of the month (1 - 31).

startday is a table of the day of the week each month starts on
(in a non-leap year).  Start by looking at a calendar and using
the day of the week each month starts on (0=Sunday, 7=Saturday).
Then, try an example and adjust all the table entries to get the
correct result.  (For example, if the date you try is a Tuesday
(2) and the equation gives you Friday (6), then you need to
subtract 4 (6-2) from each entry in the table.  If the result is
negative, just add 7.)
----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones@SDRC.UU.NET
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
"You know how Einstein got bad grades as a kid?  Well MINE are even WORSE!"
-Calvin