[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

TDateTime and StrToDate problem(Convertionerror)

Posted on 1998-11-09
9
Medium Priority
?
999 Views
Last Modified: 2008-02-20
I got a problem with the TDateTime type !!
It works fine with most Dateformats but
When windows date is set to dd-mmm-yy (09-nov-98) the
function StrToDate or DateToStr doesn't work !!
I get an EConvertionError exception and a '09-nov-98' is not
a valid date !! Is there another way to convert between String and TDateTime ??
0
Comment
Question by:Jeb
[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
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1346237
Try setting this in your FormCreate or FormShow procedures:

   ShortDateFormat := 'DD/MM/YYYY';

It _should_ convert your dates over to this format on in Delphi.

Hope it helps,

Stuart.
0
 
LVL 1

Expert Comment

by:bozo7
ID: 1346238
ShortDateFormat does not work all the time. I have had problems with it working on all machines.
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1346239
Sounds like a very strange problem.  We write software using D1, D2 and D4 which is shipped around the globe, and never have we encountered a problem like this.

Stu
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:BlackDeath
ID: 1346240
hi, jeb. hi, all.

i agree: there are certain difficulties with StrToDate / DateToStr.
at least you should make sure "DateSeparator" is set properly.
but even with this i can't get a conversion from an input like '09-nov-98' working.
all i can manage is to convert '09-11-98'.

i think the problem is indeed in ShortDateFormat. StrToDate determines the sequence of day, month and year on ShortDateFormat.
and ShortDateFormat only accepts d, dd, m, mm, yy, yyyy.
mmm is not included and thus, i'm afraid, you can't use ShortMonthNames for input to conversion from String to TDateTime.

or am i wrong with this?

alas, no big help.

Black Death.

0
 
LVL 1

Expert Comment

by:sassas081597
ID: 1346241
How about creating your own StrToDate and DateToStr routine:
function MyDateToStr(V: TDateTime): ShortString;
var d,m,y: Integer;
begin
   DecodeDate(V,d,m,y);
   if D<10 then Result:='0';
   Result:=Result + intToStr(d);
   case M of
   1: Result:=Result + 'Jan';
   2:...
   12:...
   end;
   Result:=Result+IntToStr(y);
end;

function MyStrToDate(V: String, Divider: string[1]): TDateTime;
begin
   ...
end;
0
 
LVL 1

Expert Comment

by:slautin
ID: 1346242
Why do you not use FormatDateTime function?
0
 
LVL 1

Expert Comment

by:bozo7
ID: 1346243
That seems to me that the FormatDateTime would be a good solution. As long as the date has the four digit year. If you only have the 2 digit year you lose some Y2K compatability.

One question is: Are you using the datetostr and strtodate for display purposes or for storing in a table?
0
 
LVL 1

Accepted Solution

by:
slautin earned 20 total points
ID: 1346244
Function DateToStr work fine in all cases with Windows setuped format.
About StrToDate function:
The string must consist of two or three numbers but NOT CHARACTERS, separated by the character defined by the DateSeparator global variable.
The order for month, day, and year is determined by the ShortDateFormat global variable--possible combinations are m/d/y, d/m/y, and y/m/d.

Again, pay attention to DateSeparator and ShortDateFormat.

For example:

//system date format 'dd-mmm-yy'

procedure TForm1.Button1Click(Sender: TObject);
var
   td : TDateTime;
begin
   ShortDateFormat :='dd/mm/yy';
   //Edit1.Text := '40-01-1998';
   Edit2.Text:=DateToStr(StrToDate(Edit1.Text));
   td := StrToDate(Edit2.Text);  
   Edit3.Text:=DateToStr(td); //display '04-01-98'
end;
0
 

Author Comment

by:Jeb
ID: 1346245
ShortDateFormat :='dd/mm/yy' worked !!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

649 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