Solved

calculate timespan after midnight

Posted on 2010-08-19
3
693 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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