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

x
?
Solved

Variant and TDateTime

Posted on 2009-05-13
8
Medium Priority
?
2,300 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
[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
  • 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 38

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:ISIGest
ID: 24383057
The VarType "varDouble" could not be a valid TDateTime but a normal Double value.
0
 
LVL 38

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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

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