-2

I have a situation where in I have time strings like 10:20:70 11:65:40

I need to convert them into proper time in hh:mm:ss format using c# console. For eg : 10:20:70 will be 10:21:10 after fixing 26:12:20 will be 02:12:10 as 26hours to be considered as 2 hours

How to achieve this? Please help me out.

Any help would be appreciated

  • Is this *always* going to be three two-digits number separated by colon, or is there other format? Do you want the return as string, or a proper DateTime/TimeSpan? – Martheen Apr 16 '21 at 05:10
  • @Martheen, format will be same always – Neptune_Code Apr 16 '21 at 05:11
  • Then just split them https://learn.microsoft.com/en-us/dotnet/csharp/how-to/parse-strings-using-split parse https://learn.microsoft.com/en-us/dotnet/api/system.int32.parse and distribute the overflow to the next element – Martheen Apr 16 '21 at 05:14
  • Timespan won't work for out of range elements – Neptune_Code Apr 16 '21 at 05:16
  • When I say the *return*, I mean after fixing the overflow if you want them as proper TimeSpan or just a string for display – Martheen Apr 16 '21 at 05:20
  • 2
    The link I gave you talk about treating them as regular number, not TimeSpan or DateTime – Martheen Apr 16 '21 at 05:20
  • `new TimeSpan(split[0]%24,split[1]%60,split[2]%60);` wont that be enought ? You are just looking modulo operator right `%` ? – Self Apr 16 '21 at 06:22

2 Answers2

0

Split the input and and either use a TimeSpan to get the real representation of the input.
Or use modulo operator % to fix the overflow.

var split = date.Split(":").Select(int.Parse).ToArray();
if(split.Count() != 3) {Console.WriteLine("bad format"); continue;}

/// public TimeSpan (int hours, int minutes, int seconds);
var realTimeSpanRepresentation = new TimeSpan(split[0],split[1],split[2]);          
var correctedTimeSpanRepresentation = new TimeSpan(split[0]%24,split[1]%60,split[2]%60);            

Console.WriteLine(date+" => "+realTimeSpanRepresentation+" / "+correctedTimeSpanRepresentation);        


/// public DateTime (int year, int month, int day, int hour, int minute, int second);
//var realDateTimeRepresentation = new DateTime(1,1,1,split[0],split[1],split[2]);  // Will go boom cause overflow      
var correctedDateTimeRepresentation = new DateTime(1,1,1,split[0]%24,split[1]%60,split[2]%60);

Console.WriteLine(date+" => "+correctedDateTimeRepresentation);

Result:

10:20:70 => 10:21:10 / 10:20:10
10:20:70 => 01/01/0001 10:20:10

11:65:40 => 12:05:40 / 11:05:40
11:65:40 => 01/01/0001 11:05:40

99:99:99 => 4.04:40:39 / 03:39:39
99:99:99 => 01/01/0001 03:39:39

demo: https://dotnetfiddle.net/Uwb4zc

NB: I nammed it real representation, cause Imo "00:60:00" is one Hour not "00:00:00"

Self
  • 349
  • 1
  • 8
0

Here is a method that takes a total amount of seconds and gives proper values for how many years, days, hours, minutes and seconds in there.

public static void GetTimeFromSeconds(float secondsTotal, out int s, out int m, out int h, out int d, out int y)
{
    s = m = h = d = y = 0;

    s = (int)(secondsTotal % 60);
    // substruct the seconds remainder from the total amount (75 - 15 = 60, 125 - 5 = 120).
    secondsTotal -= s;
    // if nothing left then it was less than 1 minute (45 - 0 = 45).
    if (secondsTotal < 60)
        return;

    // secondsTotal / 60 => how many minutes total
    // % 60 => how many minutes remain after splitting to whole hours
    m = (int)(secondsTotal / 60 % 60);
    // substruct the minutes remainder from the total amount (every minute takes 60 secs from the total)
    secondsTotal -= m * 60;
    // if there's not enough seconds remain in the total to get at least 1 whole hour (3600 secs)
    // then it means there was less than 1 hour.
    if (secondsTotal < 3600)
        return;

    // secondsTotal / 3600 => how many hours total
    // % 24 => what will remain after splitting to whole days (24 hours)
    h = (int)(secondsTotal / 3600 % 24);
    // every whole hour takes 3600 secs from the total
    secondsTotal -= h * 3600;
    // 24 hours = 86400 seconds.
    // If there's less remaining than it was less than 24 hours.
    if (secondsTotal < 86400)
        return;

    // secondsTotal/ 86400 => how many days total
    // % 365 => how many will remain after splitting to years
    d = (int)(secondsTotal / 86400 % 365);
    // substruct whole days
    secondsTotal -= d * 86400;
    // 1 year = 31536000 secs.
    // is there enough secs remaining to get a whole year?
    if (secondsTotal < 31536000)
        return;

    y = (int)(secondsTotal / 31536000);
}

So, you could parse your time into separate values

26:70:20 => hours=26, minutes=70, seconds=20
then count the total amount of seconds:
secondsTotal = hours * 3600 + minutes * 60 + seconds
and then use the method above:

int years, days, hours, mins, secs;
GetTimeFromSeconds(secondsTotal, out secs, out mins, out hours, out days, out years);

For 26 hours, 70 mins, 20 secs the results will be days: 1, hours: 3, minutes: 10, secs: 20.
Then format it into the format you need. For example:

TimeSpan properTime = new TimeSpan(hours, mins, secs);
properTime.ToString(@"hh\:mm\:ss");
Ermiq
  • 179
  • 4