• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8439
  • Last Modified:

convert date to a number with vb.net

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
yulyos
Asked:
yulyos
  • 5
  • 4
  • 3
  • +3
1 Solution
 
psdavisCommented:
What kind of number would you like?

mmddyyyy?  yyyymmdd?  Julian?

In c# (easily translated)

int i = Convert.ToInt32( DateTime.Now.ToString( "yyyyMMdd" ));
0
 
yulyosAuthor Commented:
example of number:

22/10/2003 = 37916
0
 
_TAD_Commented:



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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
brother7Commented:
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
 
frodomanCommented:
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
 
brother7Commented:
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
 
frodomanCommented:
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
 
brother7Commented:
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
 
frodomanCommented:
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
 
brother7Commented:
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
 
yulyosAuthor Commented:
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
 
brother7Commented:
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
 
yulyosAuthor Commented:
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
 
yulyosAuthor Commented:
To everybody

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

From:
yulyos@yahoo.com
0
 
townsmaCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now