DateTime conversion to Integer

W

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
 
C

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
 
R

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
 
W

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
 
W

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
 
C

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
 
W

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
 
P

Phill W.

William said:
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.
 
W

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top