?
Solved

convert date to a number with vb.net

Posted on 2003-10-22
15
Medium Priority
?
7,532 Views
Last Modified: 2008-03-18
how can i convert date to a number with vb.net

example in vb6:

Private Sub Form_Load()
    Text1.Text = Date
End Sub
Private Sub Command1_Click()
    Dim num
    num = Format(Text1.Text, "0")
    Text2.Text = num
End Sub

thanking you in advance

From:
yulyos@yahoo.com
0
Comment
Question by:yulyos
[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
  • 5
  • 4
  • 3
  • +3
15 Comments
 
LVL 7

Expert Comment

by:psdavis
ID: 9599774
What kind of number would you like?

mmddyyyy?  yyyymmdd?  Julian?

In c# (easily translated)

int i = Convert.ToInt32( DateTime.Now.ToString( "yyyyMMdd" ));
0
 

Author Comment

by:yulyos
ID: 9600012
example of number:

22/10/2003 = 37916
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9600561



just a comment, not an answer...


another alternative is to use the microsoft standard conversion (HAHA  like anything in MS is standard).


Just for giggles look at MS excel's date to number conversion.   They convert to the number of seconds since march 01, 1972 or some other arbitrary date.  I think it's rather stupid.
0
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 
LVL 4

Expert Comment

by:brother7
ID: 9601046
It sounds like you want to return the number of days since Jan 1, 1900.
I've tried the following and it works, returning 37914.

--- Code Start ---

Dim todaySerial As DateTime = DateSerial(Today.Year, Today.Month, Today.Day)
Dim jan11900Serial As DateTime = DateSerial(1900, 1, 1)

Dim daysSinceJan11900 As Integer = todaySerial.Subtract(jan11900Serial).Days

Console.WriteLine("The number of days between {0} and {1} is {2}", _
   todaySerial, jan11900Serial, daysSinceJan11900)

--- Code End ---
0
 
LVL 42

Expert Comment

by:frodoman
ID: 9601085
I think what you're looking for is:

cdate("22/10/2003","dd/mm/yyyy").tooadate

Note that the date passed must be in the correct format based on the computer's local settings.
0
 
LVL 4

Expert Comment

by:brother7
ID: 9601189
Revision to my code...

You are looking for the number of days since Dec 30, 1899 (not Jan 1, 1900).  This explains why my previously given result was off by 2.  Revised code is below, and gives the correct answer of 37916.

--- Code Start ---

Dim todaySerial As DateTime = DateSerial(Today.Year, Today.Month, Today.Day)
Dim dec301899Serial As DateTime = DateSerial(1899, 12, 30)

Dim daysSinceDec301899 As Integer = todaySerial.Subtract(dec301899Serial).Days

Console.WriteLine("The number of days between {0} and {1} is {2}", _
   todaySerial, dec301899Serial, daysSinceDec301899)

--- Code End ---

0
 
LVL 42

Expert Comment

by:frodoman
ID: 9601268
The built-in function tooadate provides the same information w/out the need to write your own code.

...value is the number of days from midnight, 30 December 1899... (from visual studio help file for DateTimeObject.ToOADate
0
 
LVL 4

Expert Comment

by:brother7
ID: 9601516
ToOADate can be used, but one must be aware that the result is a Double.  Therefore, it's possible to have fractional days.  When counting days between dates, it's useful to make sure all dates are set to midnight for the date given.  Or alternatively, round down the result of ToOADate.

ToOADate inherently counts the days from midnight, Dec 30, 1899.  Therefore, you just need to make sure that the other date is also set to midnight.  Or like I mentioned before, round down.

Here's some sample code, illustrating the different uses of ToOADate.  I think my preferred method is the last one, ie truncating the fractional day using integer division.

--- Code Start ---

Dim todayDate as DateTime = Now ' uses current date and time
Dim oaDays as Double = todayDate.ToOADate

Console.WriteLine("Using ToOADate method on a DateTime type with a time portion gives the result {0}", oaDays)

todayDate = Today ' uses current date, with time set to 00:00:00
oaDays = todayDate.ToOADate
Console.WriteLine("Using ToOADate method on a DateTime type with a zero time portion gives the result {0}", oaDays)

todayDate = Now ' uses current date and time again
Dim wholeOADays as integer = todayDate.ToOADate \ 1 ' use integer division to get rid of fractional days
Console.WriteLine("Using ToOADate method on a DateTime type with time portion, truncating fractional days, gives the result {0}", wholeOADays)

--- Code End ---

0
 
LVL 42

Expert Comment

by:frodoman
ID: 9601799
Ah, brother7 - I'm not trying to start a battle of one-upmanship :-)  

Nothing at all wrong with your methods and good observation about the Double-type result.

I assumed (perhaps incorrectly) that yulyos is probably working with a database that needs this format and may not necessarily want to truncate the time portion.

Cheers!
0
 
LVL 4

Expert Comment

by:brother7
ID: 9601995
I tip my hat to you, frodoman, for pointing out the ToOADate method.
I knew what needed to be done, but I didn't know there was a built-in method that does it already.  I'm familiar with the Excel way of doing this, so I had to hunt to discover a VB.NET way.

I make the Double vs Integer observation because in yulyos's followup comment, he gives an example with an integer result.  Intuitively, it makes more sense te express the difference between dates as whole days, not fractional ones.  But it really depends on the application, I guess.

I think I smell an Accepted Answer with Assist :)
0
 

Author Comment

by:yulyos
ID: 9604620
To frodoman

can you write me a small example
with The built-in function ToOADate

to convert the next line to a nomber:
TextBox1.Text = "11/06/1950"

thanking you in advance
From:
yulyos@yahoo.com
0
 
LVL 4

Accepted Solution

by:
brother7 earned 750 total points
ID: 9605078
I'm not frodoman, but here's your answer.

--- Code Start ---

      Dim dateText As String
      Dim convertedDateText As DateTime
      Dim oaDays As Integer

      dateText = "11/06/1950"
      convertedDateText = DateTime.Parse(dateText)

      Console.WriteLine("Month = {0}, Day = {1}, Year = {2}", _
         convertedDateText.Month, convertedDateText.Day, convertedDateText.Year)

      oaDays = convertedDateText.ToOADate

      Console.WriteLine("Number of days since Dec 30, 1899 = {0}", oaDays)

--- Code End ---

The above program prints out the following:
   Month = 11, Day = 6, Year = 1950
   Number of days since Dec 30, 1899 = 18573
0
 

Author Comment

by:yulyos
ID: 9605434
to brother7

 Dim MyDate As DateTime
 Dim number As Double
 MyDate = "11/06/50"
 number = MyDate.ToOADate()
TextBox1.Text = number

but accept your answer

From:
yulyos@yahoo.com

0
 

Author Comment

by:yulyos
ID: 9608427
To everybody

TextBox2.Text = Date.Parse(TextBox1.Text).ToOADate

From:
yulyos@yahoo.com
0
 
LVL 6

Expert Comment

by:townsma
ID: 12368056
The method of ToOADate is the prefered method to do what you require, as several others have pointed out.  I would however offer a warning to all that use this method when working with SQL Server.  The ToODDate is based on 30th Dec 1899, the double version of the date in SQL Server is based on 1st Jan 1900 (assuming using smalldatetime).  So if you try to use a double version of a date to remove the complexities of regions and cultures, you will find that the date used in your SQL will be 2 days higher than you think.   For example:

In a datetime Picker select 18 Oct 2004, convert using ToOADate and use in a select statement:

String.Format("Select * from Table Where DateReceived = {0}", DateVar.ToOADate().ToString())

If you then run this against an SQL Server, you would get all records dated 20 Oct 2004.

To fix this adjust the date by two days. i.e.

String.Format("Select * from Table Where DateReceived = {0}", DateVar.AddDays(-2).ToOADate().ToString())

This will then work correctly.

Note: If you use a stored procedure and actually pass a datetime as a parameter, thisis not a problem.

0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

770 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