Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to read a date in a specified format

Posted on 1998-12-07
9
Medium Priority
?
146 Views
Last Modified: 2012-05-04
I am using Turbo Pascal for DOS ver 7.0

I would like to read a date from user as DD/MMM/YY where DD is the Day and MMM is the first three characters of the month and YY is the year ie 98.  As user enters DD I would like to move cursor after the '/' etc.  Then  I would like to I would like to display how many days since first of Jan that year.  Ie.  if user enters 12/Feb/98 I would display 43 days of yaer 98 so far.

I wonder if this is possible in Pascal ( I am do VB programming and have some Pascal background).  Really I am after some code that can actually do rhis without using assembler

Please if posting code add comments.  You can send code to hshhussein@mcmail.com or post it here

Thank you all    
0
Comment
Question by:xasan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 5

Expert Comment

by:scrapdog
ID: 1216362
Not only is this possible, it shouldn't be too difficult.  However, you won't be able to do it using readln.  Your best bet would be to use readkey, the manual echo the key pressed to the screen.  You can use gotoxy to position the cursor at the point on the screen where the character goes.  Once a key is pressed, increment the position, and if there is a / in that position, juts increment two positions.  Check your help file on gotoxy and readkey (they are both in the crt unit I think).

To get the julian date, you will first have to determine if the current year is a leap year.  Do this by taking the year mod 4.  If it is 0, (with the possible exception of the year being 00), it is a leap year.

Make an array (constant) containing the julian date of the begin of each month (i.e. month 1 = 1, month 2 = 32, month 3 = 50, month 4 = 81, etc.).  If you call this array montharray, you could calculate the day of the year using this formula

day := montharray[monthnum]+dayofmonth;
if leapyear and monthnum>2 then day := day + 1;

0
 

Author Comment

by:xasan
ID: 1216363
ScrapDog

Thank you very much for the idea and I will attempt it.  However, I do not think I will be able to do it.  My Pascal is very basic.  If you could give me code to do what you said I would be so grateful.  Or at least how to read the date date from the user using ReadKey and also validating it

Thanks again - I know I am asking too much, but that is how desperate I am.

Thanks once more
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1216364
Damn, I just wrote a program that would do exactly what you wanted.  But my computer crashed, and I forgot to save the damn thing!!!

I will rewrite it but I am not in the mood right now.  I will get back to you later.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:xasan
ID: 1216365
Sorry ScrapDog but I have to let others answer it because I need it urgently.

It is still open to you so please try again



0
 
LVL 5

Accepted Solution

by:
scrapdog earned 200 total points
ID: 1216366
Here it is.  I had to rewrite it, so needless to say I put a lot of time into it.  It isn't PERFECT, but see how good it works for you.  You will probably have to make a few alterations here and there to fit your needs.
--------------------------------

uses crt;

const
  monthstring = 'JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC';
  daysinmonth :array[1..12] of integer =
                (31,28,31,30,31,30,31,31,30,31,30,31);


var month, day, year :string;

procedure keytoday(key :char;  var count :integer);
begin
  if (count=1) and not (key in ['0'..'3']) then exit;
  if key = #8 then begin  count := 1;  day := '  ';  exit; end;
  if not (key in ['0'..'9']) then exit;
  day[count] := key;  count := count + 1;
end;

procedure keytomonth(key :char;  var count :integer);
var temp :string; mpos :integer;
begin
  if (key = #8) then begin
     month[count-2] := ' ';
     count := count - 1;
     exit;
  end
  else begin
    if (count=3) and (upcase(key) in ['A','N']) then begin
      month[1] := upcase(key); count := 4;  exit;
    end;
    temp := month;
    temp[count-2] := upcase(key);
    temp := copy(temp,1,count-2);
    mpos := pos(temp,monthstring)-1;
    if (mpos mod 3) = 0 then begin
      month[count-2] := upcase(key);
      count := count + 1;
    end;
  end;
end;

procedure keytoyear(key :char;  var count :integer);
begin
  if key = #8 then begin year[count-5] := ' ';  count := count - 1;  exit; end;
  if not (key in ['0'..'9']) then exit;
  year[count-5] := key;
  count := count + 1;
  exit;
end;

var
  key :char;
  count, i, monthnum, numday, numyear, dayofyear, code, leapyear :integer;
begin
  count := 1;
  month := '   ';  day := '  ';   year := '  ';
  while (count<=7) do begin
    key := readkey;
    if upcase(key)='X' then exit;
    if count < 3 then keytoday(key,count)
    else if count < 6 then keytomonth(key,count)
    else keytoyear(key,count);
    gotoxy(1,wherey);
    write(day+'/'+month+'/'+year);
  end;
  val(day, numday, code);
  monthnum := (pos(month,monthstring)-1) div 3 + 1;
  if numday>daysinmonth[monthnum] then numday := daysinmonth[monthnum];
  val(year, numyear, code);
  leapyear := 0;
  if ((numyear mod 4) = 0) and (numyear <> 0) then leapyear := 1;
  dayofyear := 0;
  for i := 2 to monthnum do
    dayofyear := dayofyear + daysinmonth[i-1];
  dayofyear := dayofyear + numday;
  if monthnum>2 then dayofyear := dayofyear + leapyear;
  writeln;
  writeln('Day of year:  ',dayofyear);
  readln;
end.

0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1216367
Are you there?
0
 
LVL 1

Expert Comment

by:bobbyo
ID: 1216368
This is a small routine that I have used in the passed have a look.

Uses Crt;

Var

   Date : String;
   Mask : String;

Procedure ReadString(X,Y : Byte;Var S : String; MaxLength : Byte);
Var
   Ch : Char;
   TmpString : String;
   WasSlash : Boolean;
Begin
   TmpString := '';
   GotoXY(X,Y);
   Repeat
      Repeat
         Ch := UpCase(Readkey);
      Until (Ch In ['A'..'Z',#13,#8,'0'..'9']);
      WasSlash := False;
      If Ch = #8 Then
            If TmpString[Length(TmpString)] = '/'
               Then Delete(TmpString,Length(TmpString)-1,2)
               Else Delete(TmpString,Length(TmpString),1);
      Case Length(TmpString) Of
        0..1 : If Ch In ['0'..'9'] Then
               Begin
                  TmpString := TmpString + Ch;
               End;
       3..5 : If Ch In ['A'..'Z'] Then
              Begin
                  TmpString := TmpString + Ch;
              End;
       6..8 : If Ch In ['0'..'9'] Then
              Begin
                  TmpString := TmpString + Ch;
              End;
      End;
      If Length(TmpString) = 2 Then TmpString := TmpString + '/';
      If Length(TmpString) = 6 Then TmpString := TmpString + '/';
      GotoXY(X,Y);
      Write('         ');
      GotoXY(X,Y);
      Write(TmpString);
   Until Ch = #13;
   S := TmpString;
End;

Begin
   ReadString(1,1,Date,9);
End.
0
 

Author Comment

by:xasan
ID: 1216369
Scrapdog,

Sorry, Christmas shopping is taking all my time.  Thanks for the answer, I learned a lot from it and I have actually modified a little to suit me.  
0
 

Author Comment

by:xasan
ID: 1216370
Bobbyo

Your routine is excellent and I am trying to incorporte it into my program.  Thanks.  You made it all look aesy!!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In this article, I’ll show how research, determination, and use of modern technology helped me solve a DNA mystery.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question