如何在c#中修复时间字符串

问题描述

我有这样的情况,我有时间字符串 10:20:70 11:65:40

我需要使用 c# 控制台将它们转换为 hh:mm:ss 格式的正确时间。 例如:修复后 10:20:70 将是 10:21:10 26:12:20 将 02:12:10 作为 26 小时被视为 2 小时

如何实现?请帮帮我。

任何帮助将不胜感激

解决方法

拆分输入并使用 TimeSpan 来获取输入的真实表示。
或者使用模运算符 % 来修复溢出。

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,split[0],split[2]);  // Will go boom cause overflow      
var correctedDateTimeRepresentation = new DateTime(1,split[0]%24,split[2]%60);

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

结果:

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

演示:https://dotnetfiddle.net/Uwb4zc

注意:我将其命名为真实表示,因为 Imo "00:60:00" 是一小时而不是 "00:00:00"

,

这是一种计算总秒数并给出其中多少年、天、小时、分钟和秒的正确值的方法。

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);
}

因此,您可以将时间解析为单独的值

26:70:20 => hours=26,minutes=70,seconds=20
然后计算总秒数:
secondsTotal = hours * 3600 + minutes * 60 + seconds
然后使用上面的方法:

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

在 26 小时 70 分 20 秒内,结果将为 days: 1,hours: 3,minutes: 10,secs: 20
然后将其格式化为您需要的格式。例如:

TimeSpan properTime = new TimeSpan(hours,secs);
properTime.ToString(@"hh\:mm\:ss");