Solved

calculate timespan after midnight

Posted on 2010-08-19
3
695 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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