locked
What is the simple syntax to add hours and minutes? RRS feed

  • Question

  • What is the simple syntax to add hours and minutes int he 12 hour format? Keep it simple please.

    Larry Nunez

    Wednesday, September 17, 2014 4:10 PM

Answers

  • And the types are?

    if time1,2,1a,2a are datetimes then you get all you need, in seconds, but that doesn't matter, does it?

    So for one moment forget about your requirement "result in hour and minutes", you get this by the output code, internally you work with seconds.

    diff1 = time2-time1
    diff2 = time2a-time1a
    sum=diff1+diff2

    Now display diff1, diff2 and sum as hour:minutes by

    ? SecondsAsTime(diff1)
    ? SecondsAsTime(diff2)
    ? SecondsAsTime(sum)

    Function SecondsAsTime(tnSeconds)
       SET HOURS TO 24
       SET CENTURY ON
       Return Substr(Transform(Datetime(2000,1,1,0,0,0)+tnSeconds),12,5)
    Endfunc

    If you have century on and 24 hours as your default, you won't need to set it, if you want 12 hours, you'll limit this to 12 hours max, as then AM will change to PM. If you have century off the position of the time portion will shift by 2.

    Simple enough?

    To summarize: You read and store points in times as datetimes, and differences of times and sums of these differences as seconds (integers). then the code is easy peasy and all that is "hard" is the conversion into a human readable format.

    The calculations really are simple, if you accept VFP internally works with seconds instead of a time, that won't matter at all, you higher the complexity of code needed, if you enforce VFP to work with the separate hours and minutes. You see how easy it gets, if you don't force it and simply work the way it's intended. you still get the output in human readable format. That's very general for code I got into my hands and simplified. As a developer you have to think as the machine executing your code, too, not only as the human using it, but of course also. So think of an internal and external world. human readability just has to establish in output.

    The same rule applies when I see people writing much too complex sql queries just to get the format they want as output, instead of querying the simple data and transforming it in the report in the way they need it.

    Bye, Olaf.


    Olaf Doschke - TMN Systemberatung GmbH

    http://www.tmn-systemberatung.de

    • Edited by Olaf Doschke Friday, September 19, 2014 12:12 PM
    • Marked as answer by Larry_Nuñez Saturday, September 20, 2014 6:05 PM
    Friday, September 19, 2014 9:54 AM
  • I try to reproduce your problem, but can't:

    time1 = Datetime()
    time2 = Datetime()+60

    diff1 = time2-time1

    ? SecondsAsTime(diff1)

    Function SecondsAsTime(tnSeconds)
       SET HOURS TO 24
       SET CENTURY ON
       Return Substr(Transform(Datetime(2000,1,1,0,0,0)+tnSeconds),12,5)
    Endfunc

    Shows 00:01

    Edit: Did you remove the SET CENTURY ON? If you did, I said: "If you have century off the position of the time portion will shift by 2."

    There is something simpler and independant on date settings: TTOC(datetime,2), it's just the time portion string of the datetime.

    So make it

    Function SecondsAsTime(tnSeconds)
       SET HOURS TO 24
       Return Left(Ttoc((Datetime(2000,1,1,0,0,0)+tnSeconds),2),5)
    Endfunc

    It still depends on HOURS 24, otherwise 60 seconds turn to 12:01 as 60 seconds after midnight is 12:01:00 AM

    Edit2: TTOC(datetime,1) does not have that dependency on the HOURS setting, but it won't output colons, the right 6 chars will be HHMMSS.


    Olaf Doschke - TMN Systemberatung GmbH

    http://www.tmn-systemberatung.de

    • Edited by Olaf Doschke Friday, September 19, 2014 4:13 PM
    • Marked as answer by Larry_Nuñez Saturday, September 20, 2014 6:05 PM
    Friday, September 19, 2014 3:58 PM
  • It should be inverse. Also Century Off is an unfortunate setting, as you'd want the century portion of a date in most outputs of appointments or any other dates.

    I prefer code independent of date settings, so the last version should use TTOC(dt,1):

    Function SecondsAsTime(tnSeconds)
       Return Transform(Substr(Ttoc(Datetime(2000,1,1,0,0,0)+tnSeconds,1),9,4),"@R 99:99")
    Endfunc

    Just remember, that turns 00:00 after 23:59 as it's just the time portion of a datetime string. So if you sum the work time of a week you won't get 40:00 but 16:00 for 40*60*60=144000 seconds.

    You could have an output of D:HH:MM by

    Transform(Substr(Ttoc(Datetime(2000,11,30,0,0,0)+tnSeconds,1),8,5),"@R 9:99:99")

    Bye, Olaf.


    Olaf Doschke - TMN Systemberatung GmbH

    http://www.tmn-systemberatung.de

    • Edited by Olaf Doschke Friday, September 19, 2014 5:39 PM
    • Marked as answer by Larry_Nuñez Saturday, September 20, 2014 6:05 PM
    Friday, September 19, 2014 5:35 PM
  • Olaf is answering all your questions but I don't see any Vote on his answers... 

    OK, you need more answers about the subject... so here they are:

    *----------------------------------------------------------------
    PROCEDURE Num2Time
    LPARAMETERS lnTime, lcMode
    
    IF VARTYPE(m.lcMode) <> 'C'
      lcMode = 'H'
    ENDIF
    
    LOCAL lcRetVal, lnMinutes
    
    DO CASE
    CASE m.lcMode == "M" && lnTime in minutes
      lnMinutes = ROUND(m.lnTime - 60*INT(m.lnTime/60), 0)
      lcRetVal = TRANSFORM(INT(m.lnTime/60)) + ":" + PADL(m.lnMinutes, 2, '0')
    OTHERWISE  && lnTime in hours
      lnTime = ROUND(m.lnTime, 3)
      lnMinutes = ROUND(60*(m.lnTime - INT(m.lnTime)), 0)
      lcRetVal = TRANSFORM(INT(m.lnTime)) + ":" + PADL(m.lnMinutes, 2, '0')
    ENDCASE
    
    RETURN m.lcRetVal
    
    *----------------------------------------------------------------
    PROCEDURE Time2Num
    LPARAMETERS lcTime, lcMode
    
    IF VARTYPE(m.lcMode) <> 'C'
      lcMode = 'H'
    ENDIF
    
    LOCAL lnRetVal, lnMinutes
    
    DO CASE
    CASE m.lcMode == "M" && lnTime in minutes
    OTHERWISE  && lnTime in hours
      lnRetVal = VAL(m.lcTime) + VAL(SUBSTR(m.lcTime, AT(':', m.lcTime)+1))/60.00
    ENDCASE
    
    RETURN m.lnRetVal
    

    Study the code to understand the logic, place SET STEP ON inside the code if you would need to watch some values during the calculation.

    Functions usage:

    ? Time2Num("3:25")*4   && Multiply 3:25 hours by 4 (returns number)

    ? Num2Time( Time2Num("2:55") + Time2Num("5:21"))   && Add 2:55 and 5:21 (returns character)

    etc.

    The second parameter of above functions is not fully implemented yet you may do it and post the result.

    • Marked as answer by Larry_Nuñez Tuesday, September 23, 2014 2:49 PM
    Saturday, September 20, 2014 5:58 PM
  • If you don't sepcify a mode and don't extended the method for other modes than "H", Time2Num converts a time to a number of hours, eg 3:30 is converted to 3.5.

    A wage in dollars per hour multiplied by hours gives dollars, yes.

    Simply use it and you see. Why ask?

    Bye, Olaf.


    Olaf Doschke - TMN Systemberatung GmbH http://www.tmn-systemberatung.de

    • Marked as answer by Larry_Nuñez Tuesday, September 23, 2014 9:13 PM
    Tuesday, September 23, 2014 6:36 PM
  • Just a note: Time2num and Num2time can produce results with infinite number of decimal places so the final result of more complex calculations can provide certain rounding inaccuracy.

    • Marked as answer by Larry_Nuñez Tuesday, September 23, 2014 9:13 PM
    Tuesday, September 23, 2014 7:53 PM
  • How do I subtract the result I get from: 

    mtotalhours=Num2Time( Time2Num(mhoursworked1) + Time2Num(mhoursworked2))

    I want to subtract 8 hours from this result so that I can get the total extra hours and minutes worked.

    You may subtract any number from another number:

    mtotalhours = Num2Time( Time2Num(mhoursworked1) + Time2Num(mhoursworked2) - 8 )
    or
    mtotalhours = Num2Time( Time2Num(mtotalhours) - 8 )

    Time2Num() returns number so everything is OK. 

    You may test above functions in VFP Command window.

    OTOH, I don't understand how you can get extra hours by subtracting 8 hours from some number...


    • Edited by Pavel Celba Thursday, September 25, 2014 5:25 PM one more line
    • Marked as answer by Larry_Nuñez Thursday, September 25, 2014 5:51 PM
    Thursday, September 25, 2014 5:22 PM
  • Larry,

    this is really very essential VFP knowledge.

    You get this kind of compile errors if the compiler finds function calls "CALCHORAS" and "OPARATEHORAS" in your form, but can't locate where the functions are defined.

    You're most probably missing to SET PROCEDURE to your new prg file additionally to other prg files you already had.

    Bye, Olaf.


    Olaf Doschke - TMN Systemberatung GmbH http://www.tmn-systemberatung.de

    • Marked as answer by Larry_Nuñez Friday, September 26, 2014 1:42 PM
    Friday, September 26, 2014 5:56 AM
  • Well, with the code given by Pavel, Time().

    But what do you have at hand, technically? You already have your database, tables, form, etc. don't you? So what are your inputs and what kind of output would you like?

    For example just having one difference of time2-time1, you'd not be able to sum string values to a total easily. So think that through and you may arrive at datetimes or simply integers hoding seconds. It doesn't matter what you store internally, as long as you can compute the things you want to compute and get the output you want.

    Bye, Olaf.


    Olaf Doschke - TMN Systemberatung GmbH http://www.tmn-systemberatung.de

    • Marked as answer by Larry_Nuñez Saturday, September 20, 2014 6:05 PM
    Thursday, September 18, 2014 6:02 PM

All replies

  • Using the DateTime() function you can add integers to the value returned.

    Thus

    ldt = DATETIME()  && get current date and time
    ldtp = ldt + 60   && adding 60 for the number of secs
    ? ldt  && display value
    ? ldtp  && displays previous value plus 60 secs or 1 min

    Then to get to the various parts of the time use these function calls - here returned value being displayed with the ? 

    ? hour(ldtp)
    ? MINUTE(ldtp)
    ? SEC(ldtp)

     

    The English Bob

    Wednesday, September 17, 2014 4:47 PM
    Answerer
  • This really helped. Thanks a lot! The only inconvenience I have is that when I want to know how many "hours" have passed, first I have to divide the seconds by 60 to convert it to minutes. Then I have to divide again by 60 to get the hours. If I have a difference of 1.74 hours I have to then take the .74 and multiply it by 60 to get to minutes. In this way 1.74 hours turns into 1 hour and 44.4 minutes. Is there a quicker way to get them to exact hours and minutes?

    Larry Nunez

    Wednesday, September 17, 2014 7:55 PM
  • What means "quicker" for you? One FoxPro command? Less coding for you? Better speed? You cannot have everything in FoxPro... But you may call any other engine, e.g. SQLite, SQL Server, MySQL etc. etc.

    What about this:

    ? TimeDiff("15:45", "12:33")
     3:12

    But it is slow because it is written in FoxPro...

    FUNCTION TimeDiff
    LPARAMETERS lcTime1, lcTime2
    *-- By Pavel Celba
    
    LOCAL ltTime1, ltTime2, lnDiff, lcDiff
    
    ltTime1 = DATETIME(YEAR(DATE()), 1, 1, VAL(m.lcTime1), VAL(SUBSTR(m.lcTime1, AT(':', m.lcTime1)+1)), IIF(OCCURS(':', m.lcTime1)>1, VAL(SUBSTR(m.lcTime1, AT(':', m.lcTime1, 2)+1)), 0))
    ltTime2 = DATETIME(YEAR(DATE()), 1, 1, VAL(m.lcTime2), VAL(SUBSTR(m.lcTime2, AT(':', m.lcTime2)+1)), IIF(OCCURS(':', m.lcTime2)>1, VAL(SUBSTR(m.lcTime2, AT(':', m.lcTime2, 2)+1)), 0))
    lnDiff = ROUND(ABS(m.ltTime1 - m.ltTime2), 0)
    
    lcDiff = ALLTRIM(STR(INT(m.lnDiff/3600), 10, 0)) + ':'
    lnDiff = m.lnDiff - INT(m.lnDiff/3600)*3600
    lcDiff = m.lcDiff + PADL(INT(m.lnDiff/60), 2, '0')
    lnDiff = m.lnDiff - INT(m.lnDiff/60)*60
    IF LEN(m.lcTime1) > 5 OR LEN(m.lcTime2) > 5
      lcDiff = m.lcDiff + ':' + PADL(m.lnDiff, 2, '0')
    ENDIF
    
    lcDiff = IIF(m.ltTime1 < m.ltTime2, '-', '') + m.lcDiff
    lcDiff = IIF(AT(':', m.lcDiff) < 3, ' ', '') + m.lcDiff
    
    RETURN m.lcDiff
    

    Thursday, September 18, 2014 12:23 AM
  • Thanks for this. I will try the simple, "slow" code you gave me. I will only be utilizing this on 1 machine so I don´t need it to be fast. Thanks again.

    Larry Nunez

    Thursday, September 18, 2014 1:38 PM
  • I'm getting an error message for the function you gave. Function argument value, type, or count is invalid in the ltTime1 formula.

    Larry Nunez

    Thursday, September 18, 2014 2:44 PM
  • Are you FoxPro developer? Do you have source code and FoxPro debugger?

    Does the error appear in the sample function call provided?


    • Edited by Pavel Celba Thursday, September 18, 2014 2:58 PM typo
    Thursday, September 18, 2014 2:57 PM
  • Add this after the LPARAMETERS line

    Set Asserts On
    Assert Vartype(lcTime1)+Vartype(lcTime2)="CC" ; MESSAGE "You must pass in time STRINGS into this function"

    Now when the message appears click on "Debug".

    Bye, Olaf.


    Olaf Doschke - TMN Systemberatung GmbH

    http://www.tmn-systemberatung.de

    • Edited by Olaf Doschke Thursday, September 18, 2014 3:30 PM
    Thursday, September 18, 2014 3:28 PM
  • FoxPro develepor. Yes I have the debugger. Why?

    Larry Nunez

    Thursday, September 18, 2014 3:30 PM
  • When I click on debug, it points to the next line of code. Still don't know what to do next. Help.

    Larry Nunez

    Thursday, September 18, 2014 4:16 PM
  • Debugger allows to watch all used variables and their values. 

    In this particular code the second character of variable name represents the expected data type. Olaf pointed it out in the ASSERT command already.

    And I forgot to write: Fill free to update the code as much as you need. If you have different input data type you have to do some code updates.

    More info: http://msdn.microsoft.com/en-us/library/58h8bexh(v=vs.80).aspx

    http://www.tomorrowssolutionsllc.com/Conference%20Sessions/Using%20the%20Visual%20FoxPro%20Debugger%20Effectively.pdf


    • Edited by Pavel Celba Thursday, September 18, 2014 4:27 PM links
    Thursday, September 18, 2014 4:24 PM
  • Well, the debugger gives you all kind of possibilities to step through code, inspect values, see where you came from in the callstack, etc.

    If you got here, see where you got here from. You didn't passed in time strings, but Pavels code needs time strings, in the format the TIME() function returns them. The function does not work with datetime values.

    Specify what you need exactly and you get the right code. If not, you get code you can modify to your needs. It's not hard to do. The first part of the code generated datetime values from the time strings, so if you want this function to work on datetime paramaters, delete the first part of the code and instead of computing lttime1 and lttime2 make these the parameters of the function.

    Bye, Olaf.


    Olaf Doschke - TMN Systemberatung GmbH http://www.tmn-systemberatung.de

    Thursday, September 18, 2014 4:41 PM
  • All I want to do is calculate employee hours worked. That is all. If I start work at 8:00 am till 12:45 pm

    I want VFP to calculate it exactly with the result 4:45. I worked 4 hours and 45 minutes in the morning shift. Then I want to find add the morning shift hours to the afternoon shift hours. That is all. How can I get this code? Which format would be easiest to do this, Time() or Datetime()?


    Larry Nunez


    • Edited by Larry_Nuñez Thursday, September 18, 2014 5:46 PM
    Thursday, September 18, 2014 5:44 PM
  • Well, with the code given by Pavel, Time().

    But what do you have at hand, technically? You already have your database, tables, form, etc. don't you? So what are your inputs and what kind of output would you like?

    For example just having one difference of time2-time1, you'd not be able to sum string values to a total easily. So think that through and you may arrive at datetimes or simply integers hoding seconds. It doesn't matter what you store internally, as long as you can compute the things you want to compute and get the output you want.

    Bye, Olaf.


    Olaf Doschke - TMN Systemberatung GmbH http://www.tmn-systemberatung.de

    • Marked as answer by Larry_Nuñez Saturday, September 20, 2014 6:05 PM
    Thursday, September 18, 2014 6:02 PM
  • Yes I do have the tables forms and database. This is what I want. In this order:

    A)Read time1; Read time2; Get (time2-time1) result in hour and minutes.

    B)Read time1a; Read time2a; Get (time2a-time1a) result as above.

    C)Add Results, (time2-time1) + (time2a-time1a)


    Larry Nunez

    Thursday, September 18, 2014 7:02 PM
  • And the types are?

    if time1,2,1a,2a are datetimes then you get all you need, in seconds, but that doesn't matter, does it?

    So for one moment forget about your requirement "result in hour and minutes", you get this by the output code, internally you work with seconds.

    diff1 = time2-time1
    diff2 = time2a-time1a
    sum=diff1+diff2

    Now display diff1, diff2 and sum as hour:minutes by

    ? SecondsAsTime(diff1)
    ? SecondsAsTime(diff2)
    ? SecondsAsTime(sum)

    Function SecondsAsTime(tnSeconds)
       SET HOURS TO 24
       SET CENTURY ON
       Return Substr(Transform(Datetime(2000,1,1,0,0,0)+tnSeconds),12,5)
    Endfunc

    If you have century on and 24 hours as your default, you won't need to set it, if you want 12 hours, you'll limit this to 12 hours max, as then AM will change to PM. If you have century off the position of the time portion will shift by 2.

    Simple enough?

    To summarize: You read and store points in times as datetimes, and differences of times and sums of these differences as seconds (integers). then the code is easy peasy and all that is "hard" is the conversion into a human readable format.

    The calculations really are simple, if you accept VFP internally works with seconds instead of a time, that won't matter at all, you higher the complexity of code needed, if you enforce VFP to work with the separate hours and minutes. You see how easy it gets, if you don't force it and simply work the way it's intended. you still get the output in human readable format. That's very general for code I got into my hands and simplified. As a developer you have to think as the machine executing your code, too, not only as the human using it, but of course also. So think of an internal and external world. human readability just has to establish in output.

    The same rule applies when I see people writing much too complex sql queries just to get the format they want as output, instead of querying the simple data and transforming it in the report in the way they need it.

    Bye, Olaf.


    Olaf Doschke - TMN Systemberatung GmbH

    http://www.tmn-systemberatung.de

    • Edited by Olaf Doschke Friday, September 19, 2014 12:12 PM
    • Marked as answer by Larry_Nuñez Saturday, September 20, 2014 6:05 PM
    Friday, September 19, 2014 9:54 AM
  • Thank you very much, I really appreciate this. This is what I was looking for. Haven;'t tested it yet but I know it will work. This is indeed the simple code I was looking for. Thanks again. I will let you know how this turned out for me.

    Larry Nunez

    Friday, September 19, 2014 2:57 PM
  • I tried your above code and it is not working well. Here it is how I tested it:

     lcTime1=DATETIME()

    lcTime2=DATETIME()+60

    diff1=lctime2-lctime1

    DO CalcHours

    THISFORM.TEXT1.Value=SecondsAsTime(Diff1)

    Result: 0 00:                     

    This is how it's showing in the textbox, maybe the textbox needs some formatting? But the result SHOULD say :01 which is 1 minute, help...


    Larry Nunez


    Friday, September 19, 2014 3:41 PM
  • I try to reproduce your problem, but can't:

    time1 = Datetime()
    time2 = Datetime()+60

    diff1 = time2-time1

    ? SecondsAsTime(diff1)

    Function SecondsAsTime(tnSeconds)
       SET HOURS TO 24
       SET CENTURY ON
       Return Substr(Transform(Datetime(2000,1,1,0,0,0)+tnSeconds),12,5)
    Endfunc

    Shows 00:01

    Edit: Did you remove the SET CENTURY ON? If you did, I said: "If you have century off the position of the time portion will shift by 2."

    There is something simpler and independant on date settings: TTOC(datetime,2), it's just the time portion string of the datetime.

    So make it

    Function SecondsAsTime(tnSeconds)
       SET HOURS TO 24
       Return Left(Ttoc((Datetime(2000,1,1,0,0,0)+tnSeconds),2),5)
    Endfunc

    It still depends on HOURS 24, otherwise 60 seconds turn to 12:01 as 60 seconds after midnight is 12:01:00 AM

    Edit2: TTOC(datetime,1) does not have that dependency on the HOURS setting, but it won't output colons, the right 6 chars will be HHMMSS.


    Olaf Doschke - TMN Systemberatung GmbH

    http://www.tmn-systemberatung.de

    • Edited by Olaf Doschke Friday, September 19, 2014 4:13 PM
    • Marked as answer by Larry_Nuñez Saturday, September 20, 2014 6:05 PM
    Friday, September 19, 2014 3:58 PM
  • That was it. Century was on, got it off, worked well, thanks alot!

    Larry Nunez

    Friday, September 19, 2014 4:10 PM
  • It should be inverse. Also Century Off is an unfortunate setting, as you'd want the century portion of a date in most outputs of appointments or any other dates.

    I prefer code independent of date settings, so the last version should use TTOC(dt,1):

    Function SecondsAsTime(tnSeconds)
       Return Transform(Substr(Ttoc(Datetime(2000,1,1,0,0,0)+tnSeconds,1),9,4),"@R 99:99")
    Endfunc

    Just remember, that turns 00:00 after 23:59 as it's just the time portion of a datetime string. So if you sum the work time of a week you won't get 40:00 but 16:00 for 40*60*60=144000 seconds.

    You could have an output of D:HH:MM by

    Transform(Substr(Ttoc(Datetime(2000,11,30,0,0,0)+tnSeconds,1),8,5),"@R 9:99:99")

    Bye, Olaf.


    Olaf Doschke - TMN Systemberatung GmbH

    http://www.tmn-systemberatung.de

    • Edited by Olaf Doschke Friday, September 19, 2014 5:39 PM
    • Marked as answer by Larry_Nuñez Saturday, September 20, 2014 6:05 PM
    Friday, September 19, 2014 5:35 PM
  • Ok, I now get the total hours worked. Now How do I calculate with it. Example:

    If I get total hours= 3:25; How do I multiply this by 4?


    Larry Nunez

    Saturday, September 20, 2014 2:48 PM
  • Olaf is answering all your questions but I don't see any Vote on his answers... 

    OK, you need more answers about the subject... so here they are:

    *----------------------------------------------------------------
    PROCEDURE Num2Time
    LPARAMETERS lnTime, lcMode
    
    IF VARTYPE(m.lcMode) <> 'C'
      lcMode = 'H'
    ENDIF
    
    LOCAL lcRetVal, lnMinutes
    
    DO CASE
    CASE m.lcMode == "M" && lnTime in minutes
      lnMinutes = ROUND(m.lnTime - 60*INT(m.lnTime/60), 0)
      lcRetVal = TRANSFORM(INT(m.lnTime/60)) + ":" + PADL(m.lnMinutes, 2, '0')
    OTHERWISE  && lnTime in hours
      lnTime = ROUND(m.lnTime, 3)
      lnMinutes = ROUND(60*(m.lnTime - INT(m.lnTime)), 0)
      lcRetVal = TRANSFORM(INT(m.lnTime)) + ":" + PADL(m.lnMinutes, 2, '0')
    ENDCASE
    
    RETURN m.lcRetVal
    
    *----------------------------------------------------------------
    PROCEDURE Time2Num
    LPARAMETERS lcTime, lcMode
    
    IF VARTYPE(m.lcMode) <> 'C'
      lcMode = 'H'
    ENDIF
    
    LOCAL lnRetVal, lnMinutes
    
    DO CASE
    CASE m.lcMode == "M" && lnTime in minutes
    OTHERWISE  && lnTime in hours
      lnRetVal = VAL(m.lcTime) + VAL(SUBSTR(m.lcTime, AT(':', m.lcTime)+1))/60.00
    ENDCASE
    
    RETURN m.lnRetVal
    

    Study the code to understand the logic, place SET STEP ON inside the code if you would need to watch some values during the calculation.

    Functions usage:

    ? Time2Num("3:25")*4   && Multiply 3:25 hours by 4 (returns number)

    ? Num2Time( Time2Num("2:55") + Time2Num("5:21"))   && Add 2:55 and 5:21 (returns character)

    etc.

    The second parameter of above functions is not fully implemented yet you may do it and post the result.

    • Marked as answer by Larry_Nuñez Tuesday, September 23, 2014 2:49 PM
    Saturday, September 20, 2014 5:58 PM
  • Thanks. When you say "vote on his answers", do you mean marking the answer?

    Larry Nunez

    Saturday, September 20, 2014 6:09 PM
  • No, I mean the Vote count left from the message. If you click the UpArrow above the number of Votes then your Vote says "This post was helpful to me". 

    And one or more posts which really answered your question should be marked as answers as you did already.

    • Proposed as answer by alamct Thursday, September 25, 2014 4:11 PM
    Saturday, September 20, 2014 6:20 PM
  • Thanks alot! I really appreciate this. This worked lovely. One question though...That number that Time2Num("3:25")*4 returns; Is that a correct answer as saying he worked 3 hours and 25 minutes at $4.00 an hour? Is that giving me that result in money terms?

    Larry Nunez




    • Edited by Larry_Nuñez Tuesday, September 23, 2014 3:02 PM
    Tuesday, September 23, 2014 2:48 PM
  • If you don't sepcify a mode and don't extended the method for other modes than "H", Time2Num converts a time to a number of hours, eg 3:30 is converted to 3.5.

    A wage in dollars per hour multiplied by hours gives dollars, yes.

    Simply use it and you see. Why ask?

    Bye, Olaf.


    Olaf Doschke - TMN Systemberatung GmbH http://www.tmn-systemberatung.de

    • Marked as answer by Larry_Nuñez Tuesday, September 23, 2014 9:13 PM
    Tuesday, September 23, 2014 6:36 PM
  • Just a note: Time2num and Num2time can produce results with infinite number of decimal places so the final result of more complex calculations can provide certain rounding inaccuracy.

    • Marked as answer by Larry_Nuñez Tuesday, September 23, 2014 9:13 PM
    Tuesday, September 23, 2014 7:53 PM
  • How do I subtract the result I get from: 

    mtotalhours=Num2Time( Time2Num(mhoursworked1) + Time2Num(mhoursworked2))

    I want to subtract 8 hours from this result so that I can get the total extra hours and minutes worked.???


    Larry Nunez


    • Edited by Larry_Nuñez Thursday, September 25, 2014 4:10 PM
    • Proposed as answer by alamct Thursday, September 25, 2014 4:12 PM
    Thursday, September 25, 2014 4:06 PM
  • If you are looking for 12 hours format  then  ......Simple way to convert 24 hours to 12 hours format  :


    Let say I have a sales table, It has column payment time .It is containg 24 hours format like:2012-05-14 17:25:50.907.

    Want to format like 12 hours  like 4:11Pm (for example)


    Use the following query 


    Select

    right (CONVERT(VARCHAR,p.EndTime ,100),7) AS [Paymentime]

    From

    sales..Payment p

    Output result : 7:59PM

    Thanks

    Thursday, September 25, 2014 4:18 PM
  • How do I subtract the result I get from: 

    mtotalhours=Num2Time( Time2Num(mhoursworked1) + Time2Num(mhoursworked2))

    I want to subtract 8 hours from this result so that I can get the total extra hours and minutes worked.???


    Larry Nunez

    • Proposed as answer by alamct Thursday, September 25, 2014 4:36 PM
    • Unproposed as answer by alamct Thursday, September 25, 2014 4:36 PM
    • Proposed as answer by alamct Thursday, September 25, 2014 4:37 PM
    Thursday, September 25, 2014 4:19 PM
  •  
    IF you are looking for difference between two time slot using minute then please follow

    DATEDIFF (MINUTE,te.StartTime,te.EndTime )Duration


    If the post helps you  vote me

    Thanks

    • Proposed as answer by alamct Thursday, September 25, 2014 4:37 PM
    • Unproposed as answer by EnglishBob2Editor Thursday, September 25, 2014 6:31 PM
    Thursday, September 25, 2014 4:37 PM
  • How do I subtract the result I get from: 

    mtotalhours=Num2Time( Time2Num(mhoursworked1) + Time2Num(mhoursworked2))

    I want to subtract 8 hours from this result so that I can get the total extra hours and minutes worked.

    You may subtract any number from another number:

    mtotalhours = Num2Time( Time2Num(mhoursworked1) + Time2Num(mhoursworked2) - 8 )
    or
    mtotalhours = Num2Time( Time2Num(mtotalhours) - 8 )

    Time2Num() returns number so everything is OK. 

    You may test above functions in VFP Command window.

    OTOH, I don't understand how you can get extra hours by subtracting 8 hours from some number...


    • Edited by Pavel Celba Thursday, September 25, 2014 5:25 PM one more line
    • Marked as answer by Larry_Nuñez Thursday, September 25, 2014 5:51 PM
    Thursday, September 25, 2014 5:22 PM
  •  
    IF you are looking for difference between two time slot using minute then please follow

    DATEDIFF (MINUTE,te.StartTime,te.EndTime )Duration

    alamct, you are in Visual FoxPro forum and the solution proposed by you cannot work. If you are more interested in Visual FoxPro then you may download the help file from VFPX web: http://vfpx.codeplex.com/
    Thursday, September 25, 2014 5:30 PM
  • OTOH, I don't understand how you can get extra hours by subtracting 8 hours from some number...---->

    By law no employee can work over 8 hours a day. Anything over 8 hours worked is overtime pAY.

    tHANKS FOR THE calculation, it worked fine. I should've known...


    Larry Nunez

    Thursday, September 25, 2014 5:53 PM
  • I'm getting this compile error- How do i define this in?

    Form e:\ferr\forms\mov_ponches.scx has the following errors:
        Proc./Func. CALCHORAS - Undefined
        Proc./Func. OPERATEHORAS - Undefined


    Larry Nunez

    Thursday, September 25, 2014 10:32 PM
  • Larry,

    this is really very essential VFP knowledge.

    You get this kind of compile errors if the compiler finds function calls "CALCHORAS" and "OPARATEHORAS" in your form, but can't locate where the functions are defined.

    You're most probably missing to SET PROCEDURE to your new prg file additionally to other prg files you already had.

    Bye, Olaf.


    Olaf Doschke - TMN Systemberatung GmbH http://www.tmn-systemberatung.de

    • Marked as answer by Larry_Nuñez Friday, September 26, 2014 1:42 PM
    Friday, September 26, 2014 5:56 AM