Solved

calculate timespan after midnight

Posted on 2010-08-19
3
681 Views
Last Modified: 2012-05-10
Lets say I have two date time picker controls, 1 and 2
a textbox and a button.

The datetimepickers are in a custom format-hh:mm tt- to display time only.

The below code can get the timespan between a start time and and end time.

My problem is where the start time is say 11.00PM and the stop time is 01.15AM.
I realise that this is the next day whilst the date time picker date hasn't changed at all.

How do i solve the problem?
The other problem is that the datetimepickers cannot display midnight valuess ie 00.15 for example.
How can i show midnight times in a date time picker?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim starttime As DateTime = DateTimePicker1.Value
        Dim endtime As DateTime = DateTimePicker2.Value

        Dim results As TimeSpan = starttime - endtime
        TextBox1.Text = results.Duration.ToString

    End Sub

Open in new window

0
Comment
Question by:NormanMaina
  • 2
3 Comments
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33479227
To solve your second problem, set the control's Format property as Custom, and set the CustomFormat property to HH:mm:ss tt.

0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
ID: 33479318
To solve your first problem, if it's always the next day, try this:



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim starttime As DateTime = DateTimePicker1.Value

        Dim endtime As DateTime = DateTimePicker2.Value

        If endtime < starttime then

            endtime=endtime.AddDays(1)

        End If

        Dim results As TimeSpan = starttime - endtime

        TextBox1.Text = results.Duration.ToString



    End Sub

Open in new window

0
 
LVL 12

Author Closing Comment

by:NormanMaina
ID: 33479361
Zhaolai,i like your thinking!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now