TimeSpan.Parse time format hhmmss
Asked Answered
J

6

22

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?

Jiva answered 2/12, 2009 at 15:56 Comment(0)
I
35

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.

Infeasible answered 2/12, 2009 at 16:13 Comment(1)
That 24HR Time comment probably just saved me about a day of banging my head on a wall. Thanks!Conglutinate
S
12

Parse the string to a DateTime value, then subtract it's Date value to get the time as a TimeSpan:

DateTime t = DateTime.ParseExact("124510", "HHmmss", CultureInfo.InvariantCulture);
TimeSpan time = t - t.Date;
Shanika answered 2/12, 2009 at 16:2 Comment(3)
DateTime.TimeOfDay gives you a TimeSpan straight off.Curtin
You should use TimeOfDay instead of t - t.DateTesstessa
Wow, thanks. I've been using the above method all over my code, and even had a ToTime extension method at one stage. Facepalm!Belkisbelknap
C
6

You have to decide the receiving time format and convert it to any consistent format.

Then, you can use following code:

Format: hh:mm:ss (12 Hours Format)

DateTime dt = DateTime.ParseExact("10:45:10", "hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
double totalSeconds = dt.TimeOfDay.TotalSeconds;    // Output: 38170.0

Format: HH:mm:ss (24 Hours Format)

DateTime dt = DateTime.ParseExact("22:45:10", "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
double totalSeconds = dt.TimeOfDay.TotalSeconds;    // Output: 81910.0

In case of format mismatch, FormatException will be thrown with message: "String was not recognized as a valid DateTime."

Copt answered 18/12, 2014 at 5:59 Comment(0)
B
3

You need to escape the colons (or other separators), for what reason it can't handle them, I don't know. See Custom TimeSpan Format Strings on MSDN, and the accepted answer, from Jon, to Why does TimeSpan.ParseExact not work.

Belkisbelknap answered 28/4, 2013 at 11:51 Comment(0)
A
1

In case you want to work with also milliseconds like this format "01:02:10.055" then you may do as following;

public static double ParseTheTime(string givenTime)
{
var time = DateTime.ParseExact(givenTime, "hh:mm:ss.fff", CultureInfo.InvariantCulture);
return time.TimeOfDay.TotalSeconds;
}

This code will give you corresponding seconds. Note that you may increase the number of 'f's if you want to adjust precision points.

Arluene answered 21/10, 2020 at 11:16 Comment(0)
T
0

If you can guarantee that the string will always be hhmmss, you could do something like:

TimeSpan.Parse(
    timeString.SubString(0, 2) + ":" + 
    timeString.Substring(2, 2) + ":" + 
    timeString.Substring(4, 2)))
Tesstessa answered 2/12, 2009 at 16:1 Comment(1)
i cant garantee that the string will always be hhmmss. I need to put the format in a .settings fileJiva

© 2022 - 2024 — McMap. All rights reserved.