Solved

Variant and TDateTime

Posted on 2009-05-13
8
2,046 Views
Last Modified: 2012-06-27
Hi, how i can check il a "Variant" Parameter or Variable is a TDateTime or a Double?
0
Comment
Question by:ISIGest
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 24372779
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24373427
That link didn't work for me... anyway, you can check what type a variant holds, by using the VarType function... see the example below.

regards
Hypo
procedure TForm1.Button1Click(Sender: TObject);

var aVar : Variant;

    aDateTime : TDateTime;

    aDouble : Double;

begin

  aVar := aDateTime;

  Case VarType(aVar) of

    varDate: ShowMessage('Variant is a date');

    varDouble: ShowMessage('Variant is a double');

    else ShowMessage('Variant is of some other type');

  end;
 

  aVar := aDouble;

  Case VarType(aVar) of

    varDate: ShowMessage('Variant is a date');

    varDouble: ShowMessage('Variant is a double');

    else ShowMessage('Variant is of some other type');

  end;

end;

Open in new window

0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 24373433
case VarType(Param) of
  varDate: ShowMessage('Date : ' + FormatDateTime('dd/mm/yyyy hh:nn:ss', VarToDateTime(Param)));
  varDouble: ShowMessage('Double: ' + FloatToStr(Param));
end;
0
 

Author Comment

by:ISIGest
ID: 24383057
The VarType "varDouble" could not be a valid TDateTime but a normal Double value.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 24383199
how did you get the value ?
that will determine your approach !
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24383388
ISIGest: Sorry, but I really don't understand your comment... It didn't work for you?  you got a runtime error? Can you post some of your code, with an explaination of what you are trying to do, and what results you'd expect if it was working?
0
 

Author Comment

by:ISIGest
ID: 24393259
Ok...sorry...
If I test the variant variable with a VarType() function, the result can be varDouble if the variant value is a TDateTime like "15/05/2009 10.34" (in Double value format = 39948,440278), but it can be also a normal Double value like 350,25 or other value like 8540,69.
Ok, now how i can check if this value is a "valid" Double for a valid TDateTime?

Thanks.
0
 
LVL 12

Accepted Solution

by:
Hypo earned 500 total points
ID: 24393620
Ok,
so your question is actually how to identify that a double contains a value that is a valid datetime?

The problem is this, a TDateTime is in the sense of CPU, the same thing as a double, and that's the reason why the delphi compiler allows you to type myDateTime := myDouble; and vice versa. Any valid Double can also be a valid TDateTime.

So, If the code that wrote the datetime to the Variant varaible, wrote it as a double variable, then the Variant type will contain the type info varDouble, and only if the code that wrote the datetime was using a TDateTime variable, only then will the Variant contain the type info varDate.

The only things I can come up with for you to solve this are,
1:
If you have access to the code that's writing to the Variant variable, then you should make sure that it always uses a variable of type TDateTime when setting the Variants type.

2:
If you don't have access to that code, then you could perform an extra test on all Variants that you'd expect to be a datetime, but that is of type varDouble, to see that they are in va valid range. The current dates lay somewhere around 39000, each day is a new integer, so if 39000.xxx is today, then 39001 is tomorrow etc etc... If you have a range of dates that you consider to be valid, like from "Jan 1st, 1900" to "Dec 31th, 2099", then you can rule out alot of the doubles that don't contain valid dates, just by checking if the value is within that intervall. (see example below). It's not perfect, but I don't think you can get a perfect sollution for that problem, since a datetime is in some sense the same thing as a double.

regards
Hypo
Function IsValidDateTime(AVariant : Variant) : Boolean;

begin

  Case VarType(AVariant) of

    varDate: Result := True;

    varDouble: Result := (AVariant > EncodeDate(1900, 1, 1)) and (AVariant < EncodeDate(2100, 1, 1));

    else Result := False;

  end;

end;
 

procedure TForm1.Button1Click(Sender: TObject);

var A : Variant;

    B : Double;

begin

  A := Now;

  if IsValidDateTime(A)

    then ShowMessage('A contains a valid datetime')

    else ShowMessage('A does not contain a valid datetime');
 

  B := 1/2;

  A := B;

  if IsValidDateTime(A)

    then ShowMessage('A contains a valid datetime')

    else ShowMessage('A does not contain a valid datetime');
 

  B := Now;

  A := B;

  if IsValidDateTime(A)

    then ShowMessage('A contains a valid datetime')

    else ShowMessage('A does not contain a valid datetime');

end;

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

17 Experts available now in Live!

Get 1:1 Help Now