How to convert hh:mm:ss to minutes in C#

1 Answer

0 votes
using System;

class Program
{
    static double HhmmssToMinutes(string hhmmss) {
        string[] timeParts = hhmmss.Split(':');
        
        int hours = int.Parse(timeParts[0]);
        int minutes = int.Parse(timeParts[1]);
        int seconds = int.Parse(timeParts[2]);

        return (hours * 60) + minutes + (seconds / 60.0);
    }

    static void Main()
    {
        Console.WriteLine(HhmmssToMinutes("2:30:00"));
        Console.WriteLine(HhmmssToMinutes("2:35:30"));
        Console.WriteLine(HhmmssToMinutes("5:00:45"));
    }
}

   
   
/*
run:

150
155.5
300.75
   
*/

 



answered Apr 17, 2025 by avibootz

Related questions

1 answer 75 views
1 answer 84 views
1 answer 109 views
1 answer 92 views
1 answer 106 views
1 answer 96 views
1 answer 179 views
...