Sunday, October 23, 2011

Seconds to actual time in C#

I always used to have issues when debugging time related issues in AX. Time is saved in seconds so many time when you want to know the actual time associated with it in hh : mm :  ss, you will either have to do it in mind or use a calculator. In order to simplify debugging I have created a small C# based console application that takes seconds as input and returns the time in actual format.

The code is below and you can compile it in visual studio to make it work.


Download the application from here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Decimal2Hours
    class Program
        static void Main(string[] args)
            while (true)

        public static string ConvertDecimalToHours(string paramHours)
            int realPart;
            int decimalPart;
            decimal hours;
            string hoursStr;
            string minStr;
            int decimalLocation;
            string finalMinutes;
            decimal _hours = decimal.Parse(paramHours);
                hours = _hours;
                hoursStr = hours.ToString();
                decimalLocation = hoursStr.IndexOf(".");
                hoursStr = hoursStr.Substring(0, decimalLocation);
                hoursStr = hoursStr.Trim(new char[] { '.' });
                minStr = hours.ToString();
                minStr = minStr.Substring(decimalLocation);
                minStr = minStr.Trim(new char[] { '.' });
                realPart = Convert.ToInt32(hoursStr);
                decimalPart = Convert.ToInt32(Math.Floor((decimal)Convert.ToDecimal("0." + minStr) * 60));
                finalMinutes = (realPart.ToString().Length == 1 ? "0" + realPart.ToString() : realPart.ToString())
                                + ":" +
                                (decimalPart.ToString().Length == 1 ? "0" + decimalPart.ToString() : decimalPart.ToString());
                return finalMinutes;
                return paramHours;

        public static string ConvertSecondsToHoursMinutes(string paramSeconds)
            string finalTime;
            int seconds = Convert.ToInt32(paramSeconds);
            int result;
            int hours = Math.DivRem(seconds, 60 * 60, out result);
            seconds = result;
            int minutes = Math.DivRem(seconds, 60, out result);
            seconds = result;

            finalTime = hours.ToString() + " : " + minutes.ToString() + " : " + seconds.ToString();

            return finalTime;


  1. Microsoft Dynamics AX training, do keep a few things in mind to get the maximum out of the course as well as the application.we teach Microsoft Dynamics AX training and class at Hyderabad.Microsoft Dynamics CRM training in Hyderabad, Microsoft Dynamics NAV training in Hyderabad

  2. Microsoft Dynamics CRM training will help you manage and prioritize your business goals, customize.we teach MS Dynamics CRM training and class available at Hyderabad.Microsoft Dynamics AX training in Hyderabad, Microsoft Dynamics NAV training in Hyderabad

  3. Microsoft Dynamics NAV training could be a lifelong asset for your role in a small or medium enterprise.NAV Technical, NAV Trade, NAV Finance.we teach Microsoft Dynamics NAV training and class available at Hyderabad Microsoft Dynamics AX training in Hyderabad, Microsoft Dynamics CRM training in Hyderabad