in my work i get 03:00:00 as the total hours work..how can i make it in just 3?

Dim TimeOutHours = TimeSpan.FromHours(Val(DateTime.Parse(TimeOfDay.ToString(Me.TimeOutTextBox.Text.Trim)).Hour))
    Dim TimeInHours = TimeSpan.FromHours(Val(DateTime.Parse(TimeOfDay.ToString(Me.TimeInTextBox.Text.Trim)).Hour))

    Dim TimeOutMins As Integer = (Val(DateTime.Parse(TimeOfDay.ToString(TimeOutTextBox.Text)).Minute))
    Dim TimeInMins As Integer = (Val(DateTime.Parse(TimeOfDay.ToString(TimeInTextBox.Text)).Minute))

    Dim mingreat As TimeSpan = TimeOutHours.Subtract(TimeInHours).Subtract(TimeSpan.FromHours(1))
    Dim minless As TimeSpan = TimeOutHours.Subtract(TimeInHours)
    TotalHWTextBox.Text = minless.ToString

Dani AI

Generated

The "03:00:00" you were seeing is just TimeSpan's default string format. To show a single number like 3 you need to extract a numeric hour value from the TimeSpan rather than relying on its ToString. 's suggestion to cast the subtraction result is on the right track but you must convert a numeric property (for example TotalHours) — you can't cast a TimeSpan object directly. 's pointer to the Hours component also works for many simple cases, but note Hours is the hour component (0–23) and will NOT give total hours for spans longer than one day.

A robust pattern is: parse the input safely with DateTime.TryParse, handle the midnight-crossing case (if out < in then add a day), subtract to get a TimeSpan, and then read TotalHours. Use truncation or rounding depending on whether you want whole hours or fractional hours. Example in VB:

Dim inDT As DateTime
Dim outDT As DateTime

If Not DateTime.TryParse(TimeInTextBox.Text.Trim(), inDT) OrElse Not DateTime.TryParse(TimeOutTextBox.Text.Trim(), outDT) Then
    ' handle invalid input
End If

If outDT < inDT Then outDT = outDT.AddDays(1)

Dim span As TimeSpan = outDT - inDT
Dim wholeHours As Integer = CInt(Math.Truncate(span.TotalHours))
TotalHWTextBox.Text = wholeHours.ToString()

If fractional hours are required (e.g., 3.5), use span.TotalHours and format or round as needed. Also consider DateTime.TryParseExact when the input format is fixed, avoid Val and odd TimeOfDay.ToString(...) constructs in parsing, and test edge cases: midnight crossing, AM/PM input, and multi-day spans.

Recommended Answers

All 4 Replies

Change
Dim minless As TimeSpan = TimeOutHours.Subtract(TimeInHours)
to
Dim minless As Integer = CINT(TimeOutHours.Subtract(TimeInHours))

Dim minless As TimeSpan = TimeOutHours.Subtract(TimeInHours)
TotalHWTextBox.Text = minless.Hours.ToString()

thanks for answering:) it helped me

np :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.