Paul
Paul

Reputation: 47

Convert Decimal value to Hours & Minutes; Also subtract two Hours/Minutes value from the another

I have two decimal text boxes on the ASP.Net page:

Balance: 200.00 (200 Hrs and 00 Minutes)
TextBox1 = 75.30 (75 Hrs and 30 Minutes)
(After entering the value in TextBox1; the function should calculate the difference between Balance - TextBox1)
TextBox2:  (based on 60 min per hour) = 124.30 (124 Hrs and 30 minutes)

Upvotes: 1

Views: 1503

Answers (2)

BrokenGlass
BrokenGlass

Reputation: 160852

Use a TimeSpan to calculate the difference between the two values, you will have to pass in the hours and minutes separately though. In general I would avoid representing a time span as a decimal value, more commonly you see a colon as separator, i.e. 4:30.

//parse hours and minutes from textbox input

TimeSpan t1 = new TimeSpan(hours1, minutes1, 0);
TimeSpan t2 = new TimeSpan(hours2, minutes2, 0);

int deltaHours = (t1 - t2).Hours;
int deltaMinutes = (t1 - t2).Minutes;

Upvotes: 3

Andrew Kennan
Andrew Kennan

Reputation: 14157

The System.TimeSpan struct provides functions for parsing and converting times. It will also let you perform arithmetic on time values.

Upvotes: 1

Related Questions