Solved

dBase date fields

Posted on 1998-12-08
21
375 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now