?
Solved

calculate timespan after midnight

Posted on 2010-08-19
3
Medium Priority
?
698 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

771 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