473,852 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DateTime conversion to Integer

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.InvalidC astException - Cast from type 'Date'
to type 'Integer' is not valid.' error.

(Convert.Change Type(Creation_D ate, GetType(DateTim e)), Integer)

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

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***
Aug 18 '06 #1
8 1875
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

"William Foster" <no****@devdex. comschreef in bericht
news:OC******** ******@TK2MSFTN GP05.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.InvalidC astException - Cast from type 'Date'
to type 'Integer' is not valid.' error.

(Convert.Change Type(Creation_D ate, GetType(DateTim e)), Integer)

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

Yours sincerely,

William Foster

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

Aug 18 '06 #2
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:
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.InvalidC astException - Cast from type 'Date'
to type 'Integer' is not valid.' error.

(Convert.Change Type(Creation_D ate, GetType(DateTim e)), Integer)

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

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***
Aug 18 '06 #3
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 ***
Aug 18 '06 #4
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 ***
Aug 18 '06 #5
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 theDayOfThisYea r 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

"William Foster" <no****@devdex. comschreef in bericht
news:ua******** ******@TK2MSFTN GP05.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 ***

Aug 18 '06 #6
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 ***
Aug 18 '06 #7
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.Subtra ct( Date_002 ).TotalDays

If iDiff <= Array_002(i, 0 ) Then

should do that nicely.

HTH,
Phill W.
Aug 18 '06 #8
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.Da y,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 ***
Aug 18 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
118848
by: Nikola | last post by:
Hi all, I have a problem converting datetime to integer (and than back to datetime). Depending whether the time is AM or PM, same date is converted to two different integer representations, which holds as true on reversal back to datetime. AM Example:
4
4490
by: DWW | last post by:
System.DateTime returns a string with the date and time. I want to convert the time to an integer, for example, convert 10:24:45 to 102445. Much research has turned up a myriad of variations on formatting using Parse and DateTimeFormatInfo, but nothing that returns the time as an integer. Any ideas? Thanks. DW
5
2057
by: Kevin Yu | last post by:
hi all since the DateTime can't be assign null, the min value is set to 1901 or something, if in a application design, the date field can be null, so in between the UI and the DB, the business logic has to take care of the conversion?? e.g when inserting DateTime.minvalue into db, (especially when using store precedure, it seems that pass null parameter to a store precedure cause problem.), the store procedure must translate the minvalue...
26
2698
by: Reny J Joseph Thuthikattu | last post by:
Hi, I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a miniute to it.How can i do that? by manipulation i want to make '01-JUNE-2004 11:59 PM' to '02-JUNE-2004 12:00 AM' How do i do that? Reny ---
11
7262
by: Cor Ligthert | last post by:
Hello everybody, Jay and Herfried are telling me every time when I use CDate that using the datetime.parseexact is always the best way to do String to datetime conversions. They don't tell why only that I have to listen to them because they know it better. They told also that in a business situation it is better to use datetime.parseexact for changing cultures and not to use the globalization setting. I did not give them this sample,...
1
6283
by: amberz0389 | last post by:
I am trying to figure out the average time it takes to complete an event, so I need to convert my datetime to an integer. I am not sure how to do this. I was also wondering once this conversion is done, will I be able to do an average?
9
451
by: Hrvoje Voda | last post by:
How to convert text from textbox "12.05.1977" into "12/05/1977"? and also check if the text is in right format before converting? Hrcko
2
3058
by: Mike | last post by:
Hi, I have DateTime filed in db table. I'd like to search my db based on DateTime filed, actually to list all rows with particular date, for e.g. 16.11.2007... I send string to method for. eg. 16.11.2007 --short date and convert that to DateTime. After conversion it looks like 16.11.2007 0:00:00 How I can get short date like 16.11.2007 without 0:00:00 after
11
303
by: =?Utf-8?B?UGFvbG8=?= | last post by:
I have a SQL database table with rows in which the primary key is a DateTime type (although I don't use the Time part). Have added numerous rows and now want to delete one so I enter the date (dd/mm/yyyy) for a row I know is in the database but my app tells me that no row exists for that date. Am I missing something in relation to this data type?
0
9898
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10670
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7907
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5736
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5934
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.