Help with DATE$

 BBS: Inland Empire Archive
Date: 04-08-92 (04:44)             Number: 16
From: RICK PEDLEY                  Refer#: NONE
  To: GRAHAM CHATFIELD              Recvd: NO  
Subj: Help with DATE$                Conf: (2) Quik_Bas
 On 04-07-92 GRAHAM CHATFIELD wrote to All...

 GC> What I need is a date calculator to perform a function like this:
 GC>
 GC> Take todays date (April 7/92) which date$ prints as "04-07-
 GC> 1992" and find the new date if I subtract 20 days from
 GC> todays date.
 GC>
 GC> IE:    DATE$ - number_of_days = previous date
 GC>
 GC>   or   April 7/92 - 20 = March xx/92
 GC>
 GC> Hopefully it could detect if a leap year was in effect and make the
 GC> necessary adjustments for February.  Form doesn't really
 GC> matter, (mm/dd/yy or yyyy,mm,dd) as I can manipulate that
 GC> any way I choose.

DEFINT A-Z

INPUT "Date in form 02-17-1992: ", Dayt$  'or get date from DATE$
Month = VAL(MID$(Dayt$, 1, 2))
Day = VAL(MID$(Dayt$, 4, 2))
Year = VAL(MID$(Dayt$, 7, 4))
IF Month < 3 THEN
   Year = Year - 1
   Month = Month + 13
ELSE
   Month = Month + 1
END IF
JulianDay& = INT(365.25 * Year) + INT(30.6001 * Month) + Day
    '^ this converts it to a Julian date, which I believe is the
    '  number of days from the year 0.

INPUT "Subtract how many days: ", DaysSubtracted&

JulianDay& = JulianDay& - DaysSubtracted&

'Now, convert it back to a Gregorian date (the kind we're used to)
Year = INT((JulianDay& - 122.1) / 365.25)
Month = INT((JulianDay& - INT(365.25 * Year)) / 30.6001)
Day = JulianDay& - INT(365.25 * Year) - INT(30.6001 * Month)
IF Month > 13 THEN
   Month = Month - 13
ELSE
   Month = Month - 1
END IF
IF Month < 3 THEN Year = Year + 1
NewDayt$ = RIGHT$("00" + LTRIM$(STR$(Month)), 2) + "-" +
'(join to line below)
    RIGHT$("00" + LTRIM$(STR$(Day)), 2) + "-" + LTRIM$(STR$(Year))
PRINT "The new date is: "; NewDayt$

This is pieced together from various sources, and accounts for leap
years.  You might want to split the two parts into two functions,
JuliGreg and GregJuli; they'd be more useful like that.


... OFFLINE 1.36

--- Maximus 2.01wb
 * Origin: The BULLpen BBS * Intel 14.4EX (613)549-5168 (1:249/140)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET