Solved

dBase date fields

Posted on 1998-12-08
21
380 Views
Last Modified: 2010-04-04
I have a file with a dBASE date field.
I want to convert the date field
from mm/dd/yy to a string of yyyymmdd

anybody know how?
dateToStr i dont think converts it to yyyymmdd

HELP..thanks
0
Comment
Question by:waipahu
  • 9
  • 9
  • 2
  • +1
21 Comments
 
LVL 3

Expert Comment

by:williams2
ID: 1349948
It easily solved.

You can use this function:

function FormatDateTime(const Format: string; DateTime: TDateTime): string;

this example shows how to do this:

The following example assigns 'The meeting is on Wednesday, February 15, 1995 at 10:30 AM' to the string variable S.

S := FormatDateTime('"The meeting is on" dddd, mmmm d, yyyy, ' + '"at" hh:mm AM/PM', StrToDateTime('2/15/95 10:30am'));


You can also lookup FormatDateTime in the index of the Delphi Help, which provides more help.

Regards,
Williams
0
 

Author Comment

by:waipahu
ID: 1349949
THanks for the help! it works..
i got another question....
is there a function to check if the date field contains valid dates?

something like this
If ValidDate(datefield) then....


0
 
LVL 3

Expert Comment

by:williams2
ID: 1349950
If my proposal worked why in H.. did you reject it??????????????
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:waipahu
ID: 1349951
THanks for the help! it works..
i got another question....
is there a function to check if the date field contains valid dates?

something like this
If ValidDate(datefield) then....


0
 

Author Comment

by:waipahu
ID: 1349952
It did work, but how do you validate if the date field contains a valid date?
I am doing a conversion program that reads a file that contains a date field
and move that date field into a character field.  I am erroring out on the date field.
Somehow when i call the FormatDateTime function it errors out. I think
before i call the FormatDateTime function i have to validate if the content of
the date field contains a valid date.  What if its blank, or contain other stuff.

Thanks for your help!!!
0
 

Author Comment

by:waipahu
ID: 1349953
It did work, but how do you validate if the date field contains a valid date?
I am doing a conversion program that reads a file that contains a date field
and move that date field into a character field.  I am erroring out on the date field.
Somehow when i call the FormatDateTime function it errors out. I think
before i call the FormatDateTime function i have to validate if the content of
the date field contains a valid date.  What if its blank, or contain other stuff.

Thanks for your help!!!
0
 
LVL 3

Accepted Solution

by:
williams2 earned 10 total points
ID: 1349954
In the win32 page on the delphi-component menu, you'll find a component named TDateTimePicker. It will do exactly as you have planned to do. You can customize it to show either date or time with the .kind property.

You can also make it parse the userinput automatically.

Regards,
Williams
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1349955
Waipahu,

Williams2 answered your question perfectly. Go and give him his points (I think an 'A' is deserved).

At 10 points a pop you can afford to post another question. ;-)

Cheers,

Raymond.

0
 
LVL 3

Expert Comment

by:williams2
ID: 1349956
I guess Waipahu is not familiar with the site yet, let's give the guy a fair start :-)

Cheers,
Williams
0
 

Author Comment

by:waipahu
ID: 1349957
I'm Sorry, I thought if I accepted your answer you wouldn't follow up on my other concern
about validating dates.
SOrry. I really appreciate all your help. Believe it or not without your help, would
have a hard time. I am just learning Delphi and I think it is a very powerful language, problem is I just don't know all the functions and syntax and all the capabilities
of this langauge.  I know where i wanna go, just trying to get there is a bit difficult.

Thanks!

input date field (defined in a dBase file as D)  -------> validating program -----> valid date?

that is all i want to check for. Does the input date field contain a valid date.
The initial question was about formating the date, this other question is how
to validate the date field.

The input date field is coming in from external source......i am validating the data using
my delphi program.

thanks so much!
0
 

Author Comment

by:waipahu
ID: 1349958
check this program out:
if you put an invalid date in, it will ERROR off...
Put a valid date in and it will be okay.
I want a way to validate the date before calling any date conversion routines.
Thanks

procedure TForm1.Button1Click(Sender: TObject);
var
  indate: TdateTime;
begin
  indate := strtodate('02/31/98');
  showmessage(datetostr(indate));
end;

end.

0
 

Author Comment

by:waipahu
ID: 1349959
StrToDate( ) function is not Y2k compliant.

if you have a date '02/02/00'   this will be interpreted as 02/02/1900 ..how do i fix this
problem.
using Delphi 3 professional.

thanks!

0
 
LVL 3

Expert Comment

by:williams2
ID: 1349960
You should then use exception handling, this is the best way of getting the right result:

Example:

Function TForm1.testDate(DateStr: String): Boolean;
begin
  Try
    strtodate(DateStr);
    Result:= True;
  Except
    on EConvertError do Result:= False;
  End;
End;

procedure TForm1.Button1Click(Sender: TObject);
var
  indate: TdateTime;
  DateStr: String;
begin
  DateStr:= '02/31/98';
  If testDate(DateStr) then
  begin
    indate := strtodate('02/31/98');
    showmessage(datetostr(indate));
  End else
    showmessage('Please write date in the format yy:mm:dd');
end;


The procedures above will raise an exception when working in Delphi, but when you are executing outside Delphi, nobody would know even if an error happened!

Regards,
Williams
0
 
LVL 3

Expert Comment

by:williams2
ID: 1349961
..Hmmm I made a little bug, hehe:

indate := strtodate(DateStr);

0
 
LVL 3

Expert Comment

by:williams2
ID: 1349962
You may have a look in the Delphi help indexing "date/time formatting variables"

here you can change the way DateToStr and StrToDate behaves. You can then make your app. Y2K compliant

Regards,
Williams
0
 

Author Comment

by:waipahu
ID: 1349963
check this program out:
if you put an invalid date in, it will ERROR off...
Put a valid date in and it will be okay.
I want a way to validate the date before calling any date conversion routines.
Thanks

procedure TForm1.Button1Click(Sender: TObject);
var
  indate: TdateTime;
begin
  indate := strtodate('02/31/98');
  showmessage(datetostr(indate));
end;

end.

0
 

Author Comment

by:waipahu
ID: 1349964
I still got an error when i enter the Testdate function.
it will error off on the strtodate(datastr);
doesn't error trap it.
have any clue?
thanks
0
 
LVL 3

Expert Comment

by:williams2
ID: 1349965
As I said! The error is still showing when running with Delphi.

The error is gone, when you just startup the .exe file WITHOUT Delphi. Delphi does this to help the developer tracking Exceptions, even though you've allready catched it!

If that doesn't help, then read in the message, what the Exception name is and then replace it in the on ... do block.

You could also just remove the on ... do block, leaving the:
Result:= False;

Then the Try..Except block will trap all error events!

Regards,
Williams
0
 
LVL 2

Expert Comment

by:gallaghe
ID: 1349966
Consider Checking/setting the "ShortDateFormat" variable in Delphi and the "DateDelimitor" var when working with dates.
Both get info from Windows...
0
 
LVL 3

Expert Comment

by:williams2
ID: 1349967
Yes! ..But you could identify where in the variables you'll find a sting 'yy' and replace it with 'yyyy'.

This solves that problem taking care of the date/time settings for the current locale settings.

Regards,
Williams
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1349968
You should also check out the century window feature of D4, this allows you to specify how two digit dates are interpreted.

Raymond.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 68
can't find the executable in Simulator 1 100
Unique identifier on a terminal server (rdp) 4 67
FMX TEdit KeyUp handler detecting  "enter" key 4 13
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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