Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 take the format hhmmss. Any nice way to convert this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
703 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...