Results 1 to 2 of 2

Thread: [RESOLVED] [2005] TimeSpan TotalHours second shift incorrect

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    71

    [RESOLVED] [2005] TimeSpan TotalHours second shift incorrect

    I have a timesheet app that takes in two sets of time in the following format hh:mm tt

    example:

    StartTime = 1:00 AM
    EndTime = 1:15 PM
    TotalHours = 12.25

    I get these result because of the following code


    Code:
    CDate(strEndTime).Subtract(CDate(strStartTime)).TotalHours
    The issue I have is that if the following scenario happens

    example:

    StartTime = 10:21 PM
    EndTime = 2:21 AM
    TotalHours = -20.00


    Instead of it displaying 4 hours it display -20. It is possible that someone working the second shift would work from 10:21 PM to 2:21 AM.
    How can I correct this issue?
    Last edited by cedtech23; Dec 11th, 2006 at 12:00 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] TimeSpan TotalHours second shift incorrect

    If you don't specify that your end data is on the day after the start date then it is assumed that they are the same date, so the result of that calculation is exactly as it should be. Try this:
    VB Code:
    1. Dim startTime As Date 'Read is start time.
    2. Dim endTime As Date 'Read in end time.
    3.  
    4. If endTime < startTime Then
    5.     endTime.AddDays(1)
    6. End If
    7.  
    8. Dim duration As TimeSpan = endTime - startTime
    Having said that, I would think that a properly written application of this type would take into account the date as well as the time, so there shouldn't be an issue.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width