Basic Language - Date

  Pastor Joe 17:41 10 Feb 2009
Locked

I'm using a label printer that's programmed in Basic using MS Hyperterminal, no major problem as I was brought up on BASIC.

My only problem is the date. I need to print a best before date on the labels.

If I print date$ for today I get 090210

OK using Left$ & right$ I can separate them out and print them as: 10 02 09. so far so good.

I can then convert the strings to numbers using VAL and when I print them I get 10 2 9, still no problem

Now I need to add either 3 14 or 28 days depending on the product labeled. Now you're beginning to see the problem.

Can't have 38 02 09 can we?

So I need a way to add 3 14 or 28 days to the date anmd still have a valad date. I've wrecked my brains for days but haven't come up with a solution that doesn't require a couple hundred lines of code.

Currently I have a fudge in that the PC which drives the labeling machine prints todays date on the screen and asks me for the label's date. It's OK but I need to do all the 3 day products first, then the 14 and finally the 28. I also have to remember to change the date! It works but I'd like something a bit more elegant.

  Fermat's Theorem 20:44 10 Feb 2009

I don't know the exact syntax of whichever version of Basic you're using, but maybe the following procedure(s) will be of help to you.

Basically (no pun intended) I'm using 2 variables, day and month and telling the computer to establish what day is, add (in the first instance) 3 to it and then ascribe that new value to day. The condition then applied is that if day becomes greater than the number of days in the relevant month, the month variable should be incremented by one and the number of days in the month be subtracted from the variable day. So that, for example, adding 3 to 27 02 will make 03 become 03 and 28 will be subtracted from 30 - the result being 02 03.

It's been ages since I programmed in Basic, but I hope this helps you in some way. :-)


day = day +3
if day>28 and if month = 2 then month = 3:day = day-28
if day>30 and if month = 4 or month = 6 or month = 9 or month = 11 then month = month +1:day = day-30
if day>31 and if month = 1 or month = 3 or month = 5 or month = 7 or month = 8 or month = 10 then month = month + 1:day = day-31
if day>31 and if month=12 then month = 1: day=day-31
day = day + 14
if day>28 and if month = 2 then month = 3:day = day-28
if day>30 and if month = 4 or month = 6 or month = 9 or month = 11 then month = month +1:day = day-30
if day>31 and if month = 1 or month = 3 or month = 5 or month = 7 or month = 8 or month = 10 then month = month + 1:day = day-31
if day>31 and if month=12 then month = 1: day=day-31

day=day+28
if day>28 and if month = 2 then month = 3:day = day-28
if day>30 and if month = 4 or month = 6 or month = 9 or month = 11 then month = month +1:day = day-30
if day>31 and if month = 1 or month = 3 or month = 5 or month = 7 or month = 8 or month = 10 then month = month + 1:day = day-31
if day>31 and if month=12 then month = 1: day=day-31

day=day+31
if day>28 and if month = 2 then month = 3:day = day-28
if day>30 and if month = 4 or month = 6 or month = 9 or month = 11 then month = month +1:day = day-30
if day>31 and if month = 1 or month = 3 or month = 5 or month = 7 or month = 8 or month = 10 then month = month + 1:day = day-31
if day>31 and if month=12 then month = 1: day=day-31

  MAJ 20:47 10 Feb 2009

Can't you use a GOSUB to check the dates?
An INPUT to get the number of days and add them on, using a FOR, NEXT, STEP, Loop?

The GOSUB would be something like, if A (the day) > 31, then B (the month) =B+1.
If A > 30 and B=4 then B=B+1
If A > 30 and B=6 then B=B+1
If A > 30 and B=9 then B=B+1
If A > 30 and B=11 then B=B+1
If A > 28 and B=2 then B=B+1

Add a few more lines in the GOSUB to check for the year and leap year.

  natdoor 20:56 10 Feb 2009

You are probably already aware of the logic required but, just in case, I will give an brief outline. I am sorry that I cannot help with the Basic, having not used it for more than twenty years.

In logical terms, you will need to be able to process the month data. Then, for example if month = 2 and if Day + offset (3, 14, 28) > 28, then Day = Day + offset - 28.It is a matter of grouping 30 day months and 31 day months in groups with equivalent manipulation. You could also include a leap year check for processing February, if the system will be in use in 2012.

  Pastor Joe 00:16 11 Feb 2009

I think I've got it. I add the offset to date and then run it through a subroutine simmilar to the following:

Just tried it on the machine and it works.


If day>31 and month=1 then day=day-31 : month=2
If day>28 and month=2 then day=day-28 : month=3
If day>31 and month=3 then day=day-31 : month=4
If day>30 and month=4 then day=day-30 : month=5
etc

Soesn't matter how many days I add so long as it's less than 29. Of course I'll have to change Feb in a leap year but that's small beer.

At least now I can completly automate the date.

Thanks guys.

This thread is now locked and can not be replied to.

Elsewhere on IDG sites

WPA2 hack: How secure is your Wi-Fi?

Microsoft Surface Book 2 hands-on review – bigger and 5x faster

Best kids apps for iPhone & iPad

Que faire si son iPhone ou iPad est tombé dans de l'eau ?