Solved

convert date to a number with vb.net

Posted on 2003-10-22
15
6,758 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
  • 5
  • 4
  • 3
  • +3
15 Comments
 
LVL 7

Expert Comment

by:psdavis
Comment Utility
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
Comment Utility
example of number:

22/10/2003 = 37916
0
 
LVL 22

Expert Comment

by:_TAD_
Comment Utility



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
 
LVL 4

Expert Comment

by:brother7
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:brother7
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
To everybody

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

From:
yulyos@yahoo.com
0
 
LVL 6

Expert Comment

by:townsma
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now