Different results if you have String or a Varient for Dates?

  • Thread starter Thread starter Thomas Scheiderich
  • Start date Start date
T

Thomas Scheiderich

I have the following page as test.aspx:
***************************************************
<html>
<head>
<title>Hello and Welcome Page</title>
</head>
<body>
<center>
<%
Dim CurrentDate As String
CurrentDate = Today
%>
The current date is <%=CurrentDate %>.<br>
</center>
</body>
</html>
***************************************************

If I have CurrentDate as String, like I do here, I get the following
page displayed:

The current date is 2/14/2004.

But if I take out the As String and have only "Dim CurrentDate", I get
the following result:

The current date is 2/14/2004 12:00:00 AM.

Why do I get the time for the Variant and not for the string? Why we
get different results, depending whether the resulting variable is a
String or Variant. I am assuming that the time is being truncated for
the String and not for the Variant. Why?

Thanks,

Tom.
 
Thomas Scheiderich said:
Why do I get the time for the Variant and not for the string? Why we
get different results, depending whether the resulting variable is a
String or Variant. I am assuming that the time is being truncated for
the String and not for the Variant. Why?

Just a guess, but it sounds like there is are some internal implict
convertors in action here, and that they are inconsistent. This is one thing
I never liked about VB was its implicit conversions and its history of
inconsistency in them.

I recommend you always ues explicit conversion of items, especially when
converting to and from strings.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
Chad said:
Just a guess, but it sounds like there is are some internal implict
convertors in action here, and that they are inconsistent. This is one thing
I never liked about VB was its implicit conversions and its history of
inconsistency in them.

I recommend you always ues explicit conversion of items, especially when
converting to and from strings.

Someone juxt mentioned to me that there is no Variant type in VB.Net and
that if I don't put an "AS", such as:

Dim CurrentDate As String

it will default to:

Dim CurrentDate As Object

Still not sure why I get the time here, but as you say, there are
probably something internally happening here.

Thanks,

Tom.
 
Thomas Scheiderich said:
Dim CurrentDate As Object

This would make sense then. CurrentDate would be the same type as the date
time.
Still not sure why I get the time here, but as you say, there are
probably something internally happening here.

Sounds like thats how they defined conversions to strings. Again
inconsistent, and a good reason to just use explicit conversions.
 
Thomas Scheiderich said:
I have the following page as test.aspx:
***************************************************
<html>
<head>
<title>Hello and Welcome Page</title>
</head>
<body>
<center>
<%
Dim CurrentDate As String
CurrentDate = Today
%>
The current date is <%=CurrentDate %>.<br>
</center>
</body>
</html>
***************************************************

Why not:

Dim CurrentDate As DateTime
CurrentDate = DateTime.Now

And set Option Strict on.
 
John said:

What do you mean here? I am just trying to find out why today passes
back something different for:

Dim CurrentDate

and

Dim CurrentDate as String?

Dim CurrentDate As DateTime
CurrentDate = DateTime.Now

And set Option Strict on.

What does this do?

Thanks,

Tom
 
Thomas Scheiderich said:
What do you mean here? I am just trying to find out why today passes
back something different for:

Dim CurrentDate

and

Dim CurrentDate as String?



What does this do?

If "this" means "Option Strict", the answer is that "Option Strict On" at
the beginning of a file prevents most implicit conversions. You should also
use "Option Explicit On" if you don't already. It requires that you declare
all variables. Between the two of them, you will be informed of many
mistakes, like trying to treat strings and dates as the same thing.
 
John said:
If "this" means "Option Strict", the answer is that "Option Strict On" at
the beginning of a file prevents most implicit conversions. You should also
use "Option Explicit On" if you don't already. It requires that you declare
all variables. Between the two of them, you will be informed of many
mistakes, like trying to treat strings and dates as the same thing.

I have the following line at the top of my page:

<%@Page Explicit="True" Language="VB" Debug="True" %>


Should I change it to:

<%@Page Explicit="True" Strict="True" Language="VB" Debug="True" %>

Thanks,


Tom.
 
Thomas Scheiderich said:
I have the following line at the top of my page:

<%@Page Explicit="True" Language="VB" Debug="True" %>


Should I change it to:

<%@Page Explicit="True" Strict="True" Language="VB" Debug="True" %>

I use VS.NET and set Strict in the project properties, but yeah, I bet
adding Strict to the Page directive would work.

It would certainly be easy enough to try it and find out.
 
Back
Top