Standard time can be converted into decimal time using Visual Basic's built-in functions. Visual Basic works with both the date and time together. It is not possible to separate the date and time into two different independent values that can be manipulated separately. This means there is no single built-in function that will perform the task of converting the time, exclusively. However, it is still possible to do the conversion using Visual Basic's built-in functions in combination with a single line of computation.
- Skill level:
Other People Are Reading
Store the time that you want in a variable. Visual Basic has a DateTime Data type which is used to represent Date and Time values, as is described in "Murach's Visual Basic 2008." Use the Visual Basic's Dim command to declare the time as follows: Dim DateOne As DateTime = #3:15:30 AM# The Date and Time are represented as a pair, which means if you don't provide a date or time Visual Basic will substitute reference values representing the smallest date or time. Visual Basic's starting date is 12:00 AM January 1st 0001. For example, if you printed the above initialised variable to the screen, you would get '1/01/0001 3:15:30'.
Convert the DateTime variable into a decimal date and time using the built-in function ToOADate(). This built in function will convert the time to a double data type. Visit http://msdn.microsoft.com for more information. For example, the code to convert a DateTime variable into a double is: Dim ADate As DateTime = #3:15:30 AM# Dim DbleTime As Double = ADate.ToOADate() Console.WriteLine(DbleTime) This produces the following decimal date and time: '0.1357638...'
Extract the standard time from this floating value by multiplying the value to the right of the decimal point by 24 with the following code:
DbleTime *24 The result for this example should be 3.25833.
Tips and warnings
- If you also want to extract the minutes and seconds in decimal format, then multiply the number to the right of the decimal in the time by 60 to get the minutes and multiply the number to the right of the decimal in the minutes by 3600 to get the seconds. As an alternative, Visual Basic provides the DatePart() function that extracts the hour, minutes and seconds, which can be converted into decimal format as follows:
- Dim TheMinute As Double = CDbl(DatePart(DateInterval.Minute, DateOne)) / 60.0
- Dim TheSecond As Double = CDbl(DatePart(DateInterval.Second, DateOne)) / 3600.0
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for