Difference between two datetimes

S

Sheldon

Hello -

I have two dates stored in txtLogin and txtLogout textboxes. The format is
as follows:
txtLogin - 10/20/2008 8:03:00 AM
txtLogout - 10/20/2008 5:30:00 PM

I need to subract login from logout to get the total time worked for the day.

The Staffed time worked for the day needs to be displayed in a
datetimepicker (dtpStaff) control with the format of HH:mm:ss. [Done]

Also, login and logout are displayed in dtpLogin and dtpLogout with the
format of HH:mm:ss tt (the textboxes are not visible). [Done]

When login or logout are changed, the dtpStaff control needs to update.

My main problems: 1) How to subtract login from logout; 2) update the
dtpStaff control.

Thanks in advance for anyone who can be of assistance.
 
J

James Hahn

To find the difference between two times currently stored as text, use code
such as the following:

Try
Dim dt1 as DateTime.Parse(txtLogin.Text)
Dim dt2 as DateTime.Parse(txtLogout.Text)
Dim diff As TimeSpan = dt2.Subtract(dt1)
Catch e as FormatException
<error processing>
End Try

How you will update the dtpStaff control depends on where the changed start
or end time data is coming from.
 
P

Phill W.

Sheldon said:
I have two dates stored in txtLogin and txtLogout textboxes.
The format is as follows:
txtLogin - 10/20/2008 8:03:00 AM
txtLogout - 10/20/2008 5:30:00 PM

For a start, store your DateTime values in DateTime variables.

If you want the user to be able to see them then /additionally/ format
and display them.
I need to subract login from logout to get the total time worked for the day.

Dim ts as TimeSpan = dateLogout.Subtract( dateLogin )
When login or logout are changed, the dtpStaff control needs to update.

Define properties [on the Form] that allow these values to be updated,
changing both the DateTime variables holding the "real" values and
formatting the output into your TextBoxes (although you also say that
they TextBoxes are not visible, so why bother?)

Public Property LoginTime() as DateTime
Get
Return m_loginTime
End Get
Set( value as DateTime )
m_loginTime = value

Me.txtLoginText = m_loginTime.ToString( "MM/dd/yy HH:mm" )

Dim ts as new TimeSpan _
= m_logoutTime.Subtract( m_loginTime )

Me.txtElapsed.Text = ts.TotalMinutes.ToString()

End Set
End Property
Private m_loginTime as DateTime = DateTime.MaxValue

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top