How to get time balance in DTPicker

Ex -
2.30 Pm - 4.00 Pm = 2.5
dtpicker1.valuve - dtpicker2.valuve = 2.5

how to write vb6 code for this calculation
Please Help me.......

Dani AI

Generated

wanted the time difference between two DTPicker controls. The example in the original post ("2.30 Pm - 4.00 Pm = 2.5") is not correct — 2:30 PM to 4:00 PM is 1.5 hours (1 hour 30 minutes). Following the hints from and the DTPicker note from , a simple and robust VB6 approach is to read each control's Value (a Date), subtract, and convert the result (which is in days) into hours or minutes.

A minimal decimal-hours solution:

Dim t1 As Date, t2 As Date
Dim hours As Double

t1 = DTPicker1.Value
t2 = DTPicker2.Value

' Ignore differing dates if only times matter:
t1 = TimeValue(t1)
t2 = TimeValue(t2)

' If end time can be next day:
If t2 < t1 Then t2 = t2 + 1

hours = (t2 - t1) * 24
Label1.Caption = Format$(hours, "0.00")

To show hours and minutes (HH:MM):

Dim totalMinutes As Long
Dim h As Long, m As Long

totalMinutes = CLng((t2 - t1) * 24 * 60)
h = totalMinutes \ 60
m = totalMinutes Mod 60

Label1.Caption = CStr(h) & ":" & Right$("0" & CStr(m), 2)

Notes and cautions:

  • DTPicker.Value contains a date component. Use TimeValue to ignore dates or set the same date on both values before subtracting.
  • If the end time may be earlier than the start (overnight shifts), add 1 day as shown.
  • Use CLng/Format$ for stable rounding and display.
  • If the application must handle daylight-saving transitions or real-world timestamps across different dates, work with full date/time values (not just times) and validate accordingly.

Recommended Answers

All 4 Replies

In the code window, type in DateDiff and press F1 to read all about it and how to use it....

Good Luck

In the code window, type in DateDiff and press F1 to read all about it and how to use it....

Good Luck

I have not MSDN collection plese help me...

dtpicker1

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.