Legend-Lb
Legend-Lb

Reputation: 3

Calculate Time Difference in Visual Basic

I have a date/time field in a database table, i want to calculate the time difference between the date/time in table and the current date/time

For example: i want to show in the vb forum 2 Days 10 Hours Remaining

If it doesnt work for both time and date, how to do it for date only?

Thank You

Upvotes: 0

Views: 1276

Answers (2)

Dan King
Dan King

Reputation: 1110

You can also do this in the database,

SELECT (CURRENT_TIMESTAMP - '2012-12-04T20:00:00')

You can replace the hard coded date and time with a datetime column.

Upvotes: 0

Steven Doggart
Steven Doggart

Reputation: 43743

If you load the have the data already loaded into a Date (DateTime) object, then you can just do it like this:

Dim loadedDate As Date = ...
Dim span As TimeSpan = Date.Now - loadedDate
Console.WriteLine("{0} Days {1} Hours Remaining", span.Days, span.Hours)

Upvotes: 3

Related Questions