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 68 views
1 answer 81 views
1 answer 105 views
1 answer 87 views
1 answer 101 views
1 answer 93 views
1 answer 169 views
...