How to create a function like TIME2DEC

bartwyckmansbartwyckmans Member Posts: 39
edited 2004-10-13 in Navision Attain
I want to convert a time value to a decimal value...
any suggestions?

Comments

  • awarnawarn Member Posts: 261
    I think the psuedocode would look like this:

    using '12:05:10 AM'

    4 variables, for hour, minute, second and the AM/PM are filled with the 4 values

    use the EVALUATE function to make sure you get numeric numbers (not text) for the first 3.

    If PM, add 12 to the hour variable.

    Then, what do you want? A total number of seconds? This would be easy, multiple the minutes by 60 and the hours by 3600, then add them all together.

    If you want minutes multiply the hours by 60 and divide the seconds by 60 and add them all together.

    -a
  • bartwyckmansbartwyckmans Member Posts: 39
    thank you but i mean it in the other direction

    you have a variable with a time value in it.
    timeVar := 10:20:00

    i want to get the decimal value
    in this way
    decimalVar = 10,20

    so you can you convert that timeVar into a DecimalVar

    hope you can handle that problem.
  • awarnawarn Member Posts: 261
    No, that is what I meant.

    Basically you have to turn each part of your time value (which you can turn into a string using FORMAT), into a separate number and use this for a number.



    tTime := FORMAT(10:20:00)

    dHour := StrToInt(copy(tTime, 1, 2))

    dMinute := StrToInt(copy(tTime, 3, 2))

    dSecond := StrToInt(Copy(tTime, 5, 2))


    dResult := dHour + (dMinute / 60) + (dSecond / 3600)


    I didn't check this, just wrote it off the top of my head.
  • bartwyckmansbartwyckmans Member Posts: 39
    I am very very sorry
    but it doesn't work.

    It isn't possible to use the function StrToInt bcs it is not useable in Navision. So I try to use the functions EVALUATE, STRSUBSTNO, FORMAT to change the type of the timevalue into a stringformat. but none off them works!

    he always gives an error that typeconversion is not possible from Time to Text.

    Must that be possible I can follow your syntax to spilt it up in hours, minutes and seconds.

    hope you have an other suggestion
  • fbfb Member Posts: 246
    edited 2004-10-13
    If you subtract two C/AL Time variables, you get an Integer that contains the number of milliseconds between the two Times. So, you can use the following code to go from a Time to a Decimal:
    // convert time to milliseconds...
      intMilliSecondsSinceMidnight := MyTime - 000000T;
    
      // throw away seconds and milliseconds...
      intMinutesSinceMidnight := ROUND(intMilliSecondsSinceMidnight,1000*60);
    
      // convert to decimal hours (minutes represented as decimal fraction)
      decHours := intMinutesSinceMidnight / 60;
    
  • bartwyckmansbartwyckmans Member Posts: 39
    thank you this code is very useful !
Sign In or Register to comment.