TimeSpan.Parse time format hhmmss

后端 未结 6 1517
忘掉有多难
忘掉有多难 2020-12-15 20:52

in c# i have time in format hhmmss like 124510 for 12:45:10 and i need to know the the TotalSeconds. i used the TimeSpan.Parse(\"12:45:10\").ToTalSeconds but it does\'nt tak

6条回答
  •  被撕碎了的回忆
    2020-12-15 21:27

    This might help

    using System;
    using System.Globalization;
    
    namespace ConsoleApplication7
    {
        class Program
        {
            static void Main(string[] args)
            {
                DateTime d = DateTime.ParseExact("124510", "hhmmss", CultureInfo.InvariantCulture);
    
                Console.WriteLine("Total Seconds: " + d.TimeOfDay.TotalSeconds);
    
                Console.ReadLine();
            }
        }
    }
    

    Note this will not handle 24HR times, to parse times in 24HR format you should use the pattern HHmmss.

提交回复
热议问题