Home All Groups Group Topic Archive Search About

DateTime conversion to Integer

Author
18 Aug 2006 5:09 AM
William Foster
Good afternoon all,

I am trying to convert a date to an integer in Microsoft Visual Basic
2003 .NET and am experiencing a little trouble trying to get it done.

This is what I want to do:

Date_001 = Array_001(0)
Date_002 = Array_001(1)
If (Date_001 - Date_002) <= Array_002(i, 0) Then
    Value_Reference = i
    Value_Found = True
    Exit For
Else
End If

However, I have to convert the Date_001 and Date_002 variables to
Decimals or Integers before I can do this

If (System.Convert.ToDecimal(Date_001) -
System.Convert.ToDecimal(Date_002)) <= Array_002(i, 0) Then

I have tried using the Change type, but this returns a 'Run-time
exception thrown : System.InvalidCastException - Cast from type 'Date'
to type 'Integer' is not valid.' error.

(Convert.ChangeType(Creation_Date, GetType(DateTime)), Integer)

Any assistance you may be able to provide would be greatly appreciated.

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***

Author
18 Aug 2006 5:43 AM
Cor Ligthert [MVP]
William,

Can you show us with a sample date how you want to convert a DateTime to an
integer.

Therefore what should the date 2006-8-18 (in ISO notation) have to look.

A DateTime is a structure that has many members.

Cor

Show quoteHide quote
"William Foster" <nospam@devdex.com> schreef in bericht
news:OCslCSowGHA.4972@TK2MSFTNGP05.phx.gbl...
> Good afternoon all,
>
> I am trying to convert a date to an integer in Microsoft Visual Basic
> 2003 .NET and am experiencing a little trouble trying to get it done.
>
> This is what I want to do:
>
> Date_001 = Array_001(0)
> Date_002 = Array_001(1)
> If (Date_001 - Date_002) <= Array_002(i, 0) Then
> Value_Reference = i
> Value_Found = True
> Exit For
> Else
> End If
>
> However, I have to convert the Date_001 and Date_002 variables to
> Decimals or Integers before I can do this
>
> If (System.Convert.ToDecimal(Date_001) -
> System.Convert.ToDecimal(Date_002)) <= Array_002(i, 0) Then
>
> I have tried using the Change type, but this returns a 'Run-time
> exception thrown : System.InvalidCastException - Cast from type 'Date'
> to type 'Integer' is not valid.' error.
>
> (Convert.ChangeType(Creation_Date, GetType(DateTime)), Integer)
>
> Any assistance you may be able to provide would be greatly appreciated.
>
> Yours sincerely,
>
> William Foster
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
18 Aug 2006 8:22 AM
William Foster
Cor,

The data is formatted to the en-AU Region, i.e. dd/mm/yyyy; as different
to the American format of mm/dd/yyyy.

I have set the data format via the System.Convert.ToDateTime formula
using the afore mentioned region.

No matter what I do I get the InvalidCast error, I have tried quite a
few permutations of the Convert method without success.

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***
Author
18 Aug 2006 9:03 AM
Cor Ligthert [MVP]
William,

A DateTime has to keep the real date and time in its structure no known
format. It are ticks in 100th of a nanoseconds as longs starting 1-1-00.

To get a day of the year from that by instance you can say

dim theDayOfThisYear as integer = now.dayof year

(By the way in oposite of C# shows the Visual Basic debugger only dates in
USA notation)

I hope this helps,

Cor

Show quoteHide quote
"William Foster" <nospam@devdex.com> schreef in bericht
news:uaiV89pwGHA.4972@TK2MSFTNGP05.phx.gbl...
> Cor,
>
> The data is formatted to the en-AU Region, i.e. dd/mm/yyyy; as different
> to the American format of mm/dd/yyyy.
>
> I have set the data format via the System.Convert.ToDateTime formula
> using the afore mentioned region.
>
> No matter what I do I get the InvalidCast error, I have tried quite a
> few permutations of the Convert method without success.
>
> Yours sincerely,
>
> William Foster
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
18 Aug 2006 10:42 AM
William Foster
Cor,

Thanks for that, I will hopefully just be able to use a different Data
Type to the DateTime one and everything should work well.

Thank you for your assistance !

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***
Author
18 Aug 2006 8:22 AM
R. MacDonald
Hello, William,

What type is Array_002?  The Date type has a Subtract method that
returns a TimeSpan.  Perhaps it would be simpler for you to convert
Array_002 to a TimeSpan.

Cheers
Randy


William Foster wrote:
Show quoteHide quote
> Good afternoon all,
>
> I am trying to convert a date to an integer in Microsoft Visual Basic
> 2003 .NET and am experiencing a little trouble trying to get it done.
>
> This is what I want to do:
>
> Date_001 = Array_001(0)
> Date_002 = Array_001(1)
> If (Date_001 - Date_002) <= Array_002(i, 0) Then
>     Value_Reference = i
>     Value_Found = True
>     Exit For
> Else
> End If
>
> However, I have to convert the Date_001 and Date_002 variables to
> Decimals or Integers before I can do this
>
> If (System.Convert.ToDecimal(Date_001) -
> System.Convert.ToDecimal(Date_002)) <= Array_002(i, 0) Then
>
> I have tried using the Change type, but this returns a 'Run-time
> exception thrown : System.InvalidCastException - Cast from type 'Date'
> to type 'Integer' is not valid.' error.
>
> (Convert.ChangeType(Creation_Date, GetType(DateTime)), Integer)
>
> Any assistance you may be able to provide would be greatly appreciated.
>
> Yours sincerely,
>
> William Foster
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
18 Aug 2006 8:30 AM
William Foster
Randy,

Array_002 does not have a defined DateType as there are many different
DataTypes within the Array, i.e. Strings, Decimals, DateTimes, Doubles
(I think that is it).  As such I can not define it as a straight
DataType.

I need the formula to return a number of days, as different variables
have actions based on the number of days between the dates.

Thanks for your assistance !

Thanks go to Cor as well (I think I forgot to write thank you in my
reply to his/her response).

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***
Author
18 Aug 2006 11:31 AM
Phill W.
William Foster wrote:

> I am trying to convert a date to an integer in Microsoft Visual Basic
> 2003 .NET and am experiencing a little trouble trying to get it done.

I'm not surprised.  Unlike VB6 and Excel, I don't think there is a
simple conversion from one to the other.

> This is what I want to do:
>
> If (Date_001 - Date_002) <= Array_002(i, 0) Then

> However, I have to convert the Date_001 and Date_002 variables to
> Decimals or Integers before I can do this

No you don't.  The DateTime class has a Subtract method that works out
the difference between one dat and another and returns you a TimeSpan
representing that interval.  From this, you can extract the number of
days (or whatever), so

    Dim iDiff as Integer
       = Date_001.Subtract( Date_002 ).TotalDays

    If iDiff <= Array_002(i, 0 ) Then

should do that nicely.

HTH,
    Phill  W.
Author
18 Aug 2006 11:22 PM
William Foster
Phil,

Yes, you are correct !

After the feedback from Cor and the other contributor I ended up using
the inbuilt DateDiff method.

Days_Difference = DateDiff(XXX.Day,Date_001,Date_002)

I did find a method to do the convert within Visual Studio, only I can't
remember the name of the function. Basically you needed to set up a
conversion class (I am on a different machine at the moment so I don't
have access to the reference).  However, the DateDiff function is much
much easier than going through all that.

Thank you to everyone for your assistance.

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***