+ Reply to Thread
Results 1 to 6 of 6

Date not cycling with addition of time

  1. #1
    Registered User
    Join Date
    08-04-2011
    Location
    Hillsboro, OR
    MS-Off Ver
    Excel 2003
    Posts
    14

    Date not cycling with addition of time

    Trying to figure out issue with adding date and time.
    The code is listed below.
    I have watched the code while running. The initial date FDateS and FDate(t) value is m/dd/yyyy (9/16/2010) and no time. FDateS has different amounts of time added to it. The issue I have encountered is that once it passes midnight, the date does not change but the time reflects the change to 00:00:00.

    Public FDate(1000) As Date
    Public FTime(1000) As Date


    FDate(t) = CDate(Mid(FindData("sm_Sys_BatchOpenTime"), 1, 10))

    For H1 = 1 To n
    FDateS = FDate(H1)
    For r =1 to 50
    FDateS = CDate(FDateS) + CDate(FTime(r)-Ftime(r-1))
    Next
    Next

  2. #2
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Date not cycling with addition of time

    Cummins;
    I'm guessing that the values in FTime(r) and Ftime(r-1) differ by less than 1 (24 hours). When you add a value less than 1 it won't add a day onto FDateS.
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  3. #3
    Registered User
    Join Date
    08-04-2011
    Location
    Hillsboro, OR
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Date not cycling with addition of time

    Quote Originally Posted by foxguy View Post
    Cummins;
    I'm guessing that the values in FTime(r) and Ftime(r-1) differ by less than 1 (24 hours). When you add a value less than 1 it won't add a day onto FDateS.
    The FTime delta is less than 24 hours but after adding all of the deltas (r) together it rolls through 24:00. The data actually displays as follows (There is more but I don't want to bore you):

    9/16/2010 23:00
    9/16/2010 23:30
    9/16/2010 0:00
    9/16/2010 0:30
    9/16/2010 1:00
    9/16/2010 1:30

  4. #4
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Date not cycling with addition of time

    Whoops, I misread your code.

    Assuming that
    r1 = 23:00
    r2 = 23:30
    r3 = 00

    FTime(r3)-FTime(r2) = -23:30 (Negative), so it subtracts 23:30 instead of adding 0:30.

    try CDate(FTime(r)-FTime(r-1)+iif(r<r-1,1,0))

  5. #5
    Registered User
    Join Date
    08-04-2011
    Location
    Hillsboro, OR
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Date not cycling with addition of time

    Quote Originally Posted by foxguy View Post
    Whoops, I misread your code.

    Assuming that
    r1 = 23:00
    r2 = 23:30
    r3 = 00

    FTime(r3)-FTime(r2) = -23:30 (Negative), so it subtracts 23:30 instead of adding 0:30.

    try CDate(FTime(r)-FTime(r-1)+iif(r<r-1,1,0))


    I wasn't quite sure of how your code worked but, I used the following and it fixed it.
    If FTime(r) > FTime(r - 1) Then
    FDateS = FDateS + (FTime(r) - FTime(r - 1))
    Else
    FDateS = FDateS + (FTime(r) - FTime(r - 1)) + CDate(1)
    End If

  6. #6
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Date not cycling with addition of time

    Same thing, just split into more lines.
    BTW: You don't need "CDate(1)", just "1". Adding 1 to a date increases it 1 day (same as CDate(1)).

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1