Convert real number to time string


While I was developing a report for payroll module, We had done some customization for storing employee's total working hours in a separate table and I had to develop a report for employee's time sheet to show his/her time in and time out.

As I was getting total time in hour [Real number] so I had to change real hour to time and then calculate employee's time in and time out for each day of the month.

Here is the code to convert real hour to time.

static void realHour2Time(Args _args)
{
    int     hours, minutes;
    real    fractionalPart, hourInReal = 7.3555;
    str     minStr, timeStr;

    if (hourInReal != 0)
    {
        hours = trunc(hourInReal );
        fractionalPart = hourInReal - hours;
        minutes = fractionalPart*60;
        if (strLen(strFmt("%1", minutes)) == 1)
        {
            minStr = strFmt("0%1", minutes);
        }
        else
        {
            minStr = strFmt("%1", minutes);
        }

        if (hourInReal < 10)
        {
            timeStr = strFmt("0%1:%2", hours, minStr);
        }
        else if(hourInReal >= 10)
        {
            timeStr = strFmt("%1:%2", hours, minStr);
        }
    }
    else
    {
        timeStr = strFmt("%1", 0);
    }
    info(strfmt("Converted time: %1", timeStr));
}

Output:
Converted time: 07:21



Comments

Popular posts from this blog

🚀 Export All D365 F&O Custom Models with a Single Batch File

SQL stored procedure to get Item's on hand on specified date