0

I have a time class that has functions to compute hours, mins and secs individually .I want to convert the calculated time(which is of format 1h 30min 20secs obtained from toReadableString() method in the code below) into HH;MM:SS format. I have seen some examples but that doesn't solve my problem.Also, I want to calculate total time duration by adding two different time slots(for eg.1st time slot is 20mins 45secs and 2nd time slot is 1hr30mins15secs which adds together to give 1hr51mins).Any help is appreciated.Thanks in advance.

   public int getMinutes()
    {
        long minutesTotal = (time / 1000 / 60);

        return (int)minutesTotal % 60;
    }


   public int getSeconds()
    {
        return (int)(time - (getHours() * 60 * 60 * 1000) - (getMinutes() * 60 * 1000)) / 1000;
    }

    public int getHours()
    {
        return (int)(time / 1000 / 60 / 60);
    }
   public String toString()
    {
        return "abs_" + Convert.ToString(time);
    }

    /**
    * Convert time to a human readable string.
    * @return - human readable string.
    */
    public String toReadableString()
    {
        if (getHours() == 0 && getMinutes() == 0 && getSeconds() == 0)
        {
            if (getTime() > 0)
            {
                return getTime() + "ms";
            }
            else
            {
                return "zero";
            }
        }

        String result = "";
        if (getHours() != 0)
        {
            result += Convert.ToString(getHours()) + "h ";
        }
        else
        {
            if (getMinutes() == 0)
            {
                return Convert.ToString(getSeconds()) + "sec";
            }
        }
        result += Convert.ToString(getMinutes()) + "m ";
        result += Convert.ToString(getSeconds()) + "s";
        return result;
    }
jjj
  • 1,136
  • 3
  • 18
  • 28
  • Possible duplicate of [Convert dateTime to ISO format yyyy-mm-dd hh:mm:ss in C#](http://stackoverflow.com/questions/1912894/convert-datetime-to-iso-format-yyyy-mm-dd-hhmmss-in-c-sharp) – chadnt Feb 13 '17 at 22:02
  • 1
    what is the data type of time , int ? – jjj Feb 13 '17 at 22:04
  • Possible duplicate of [Given a DateTime object, how do I get an ISO 8601 date in string format?](http://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-format) – jjj Feb 13 '17 at 22:05
  • Your code looks quite unnecessarily complex, and also you are calling those getters many times. Why doesn't `toReadableString` simply check `time > 0`? And you shouldn't use a string as a way to store a time... Anyway, I think you should make some basic checks to see what parts are in the string and then call `TimeSpan.Parse`, `TimeSpan.ParseExact`, `DateTime.Parse` or `DateTime.ParseExact`. Or else you could just replace `: ` and `min ` with `:` and `secs` with an empty string. – Andrew Feb 13 '17 at 22:07
  • Also, I think this question would really help: http://stackoverflow.com/questions/463642/what-is-the-best-way-to-convert-seconds-into-hourminutessecondsmilliseconds/41799528 – Andrew Feb 13 '17 at 22:13
  • @Andrew I accept my code is complex. I am doing a big app where many of my GUI forms needs different layouts , that's why I am converting time to user readable string.Thanks anyway for your suggestion. – user7274707 Feb 15 '17 at 22:44
  • It's ok to be complex, but not to be unnecessarily complex, that's what I meant. Your code can be highly improved and reduced, like directly checking for `time`, and saving the results of methods like `getTime` to avoid calling them 3 times, among other things. Also, in my link, check for my answer, in which you will see the best way to convert milliseconds to a readable string. – Andrew Feb 16 '17 at 02:42

1 Answers1

2

In C# you can use TimeSpan to do Math on these time values.

Try this:

 var first = new TimeSpan(0, 20, 45);    // 20mins 45secs 
 var second = new TimeSpan(1, 30, 15);   // 2nd time slot is 1hr30mins15secs
 var result = first + second;

 Debug.WriteLine(result.ToString());

.

 public String toReadableString(TimeSpan ts)
 {
     // TODO: Write your function that receives a TimeSpan and generates the desired output string formatted...
     return ts.ToString();    // 01:51:00
 }
Tony
  • 16,527
  • 15
  • 80
  • 134