如何在C#中添加时间?

时间:2021-03-09 23:08:50

How do I add times in C#? For example:

如何在C#中添加时间?例如:

Time = "14:20 pm" +  "00:30 pm"

6 个解决方案

#1


Assuming you want to add 30 minutes to a given DateTime, you can use AddMinutes.

假设您要为给定的DateTime添加30分钟,可以使用AddMinutes。

TestTime.AddMinutes(30);

Another way of doing it:

另一种方式:

DateTime TestTime = DateTime.Parse("22 Jun 2009 14:20:00");
// Add 30 minutes
TestTime = TestTime + TimeSpan.Parse("00:30:00");

#2


You would want to convert both times into a TimeSpan objects.

您可能希望将两次转换为TimeSpan对象。

This will give you explicit access to the Hours/Minutes values of each time and you can add them together.

这将使您可以显式访问每次的小时/分钟值,并可以将它们一起添加。

See TimeSpan from MSDN.

请参阅MSDN中的TimeSpan。

#3


 TimeSpan t1 = new TimeSpan(14, 20,0);
 TimeSpan t2 = new TimeSpan(0,30,0);
 Console.Out.WriteLine(t1 + t2);

#4


Try this (although 0:30pm doesn't make sense):

试试这个(虽然下午0:30没有意义):

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(new StringTime("14:20 pm").Add(new StringTime("0:30 pm")));
        Console.WriteLine(new StringTime("15:00 pm").Add(new StringTime("0:30 pm")));
        Console.WriteLine(new StringTime("5:00am").Add(new StringTime("12:00pm")));
    }
}

class StringTime
{
    public int Hours { get; set; }
    public int Minutes { get; set; }
    public bool IsAfternoon { get; set; }

    public StringTime(string timeString)
    {
        IsAfternoon = timeString.Contains("pm");
        timeString = timeString.Replace("pm", "").Replace("am", "").Trim();

        Hours = int.Parse(timeString.Split(':')[0]);
        Minutes = int.Parse(timeString.Split(':')[1]);
    }

    public TimeSpan ToTimeSpan()
    {
        if (IsAfternoon)
        {
            if (Hours < 12)
            {
                Hours += 12;
            }
        }
        return new TimeSpan(Hours, Minutes, 00);
    }

    public TimeSpan Add(StringTime time2)
    {
        return this.ToTimeSpan().Add(time2.ToTimeSpan());
    }
}

Output (the value before the dot are days):

输出(点前的值是天):

1.02:50:00
1.03:30:00
17:00:00

1.02:50:00 1.03:30:00 17:00:00

#5


You can't add those, just like you can't add "14:20PM" and the color red. You can add a time and a timespan (14:20PM + 30 minutes) or two timespans (2 hours+30 minutes). But you cannot add two times.

你不能添加那些,就像你不能添加“14:20 PM”和红色。您可以添加时间和时间跨度(下午14:20 + 30分钟)或两次时间(2小时+30分钟)。但你不能添加两次。

To make this even clearer, consider what would happen if you could add two times: 14.20 + 00:30 (EST) = 23.20 + 09:30 (UTC)

为了使这一点更加清晰,请考虑如果可以添加两次会发生什么:14.20 + 00:30(EST)= 23.20 + 09:30(UTC)

#6


This code enables a user to add times.

此代码使用户能够添加时间。

using System;
using System.Collections.Generic;

// ...
class Program
{
    static void Main(string[] args)
    {
        Console.ForegroundColor = ConsoleColor.Cyan;
        TimeCalculator time = new TimeCalculator();
        time.InsertTime(0, 0, 59);
        time.InsertTime(0, 0, 1);

        int[] totaltime = time.GetTime();

        Console.WriteLine($"Hours: {totaltime[0]}");
        Console.WriteLine($"Minutes: {totaltime[1]}");
        Console.WriteLine($"Seconds: {totaltime[2]}");

        Console.ForegroundColor = ConsoleColor.Gray;
        Console.Write("Press any key to quit . . . ");
        Console.ReadKey(true);
    }
}

class TimeCalculator
{
    int _hours;
    int _minutes;
    int _seconds;
    List<TimeCalculator> timelist = new List<TimeCalculator>();

    public void InsertTime(int hours, int minutes, int seconds)
    {
        timelist.Add(new TimeCalculator { _hours = hours, _minutes = minutes, _seconds = seconds });
        AddTime();
    }

    public void AddTime()
    {
        int sumHours = 0;
        int sumMinutes = 0;
        int sumSeconds = 0;

        foreach (var item in timelist)
        {
            sumHours += item._hours;
            sumMinutes += item._minutes;
            sumSeconds += item._seconds;
        }

        _hours = sumHours + (sumMinutes / 60);
        _minutes = (sumMinutes + (sumSeconds / 60)) % 60;
        _seconds = sumSeconds % 60;

        // % 60 restarts clock at 60
        // / 60 gives value at 60
    }

    public int[] GetTime()
    {
        return new int[] { _hours, _minutes, _seconds };
    }
}

#1


Assuming you want to add 30 minutes to a given DateTime, you can use AddMinutes.

假设您要为给定的DateTime添加30分钟,可以使用AddMinutes。

TestTime.AddMinutes(30);

Another way of doing it:

另一种方式:

DateTime TestTime = DateTime.Parse("22 Jun 2009 14:20:00");
// Add 30 minutes
TestTime = TestTime + TimeSpan.Parse("00:30:00");

#2


You would want to convert both times into a TimeSpan objects.

您可能希望将两次转换为TimeSpan对象。

This will give you explicit access to the Hours/Minutes values of each time and you can add them together.

这将使您可以显式访问每次的小时/分钟值,并可以将它们一起添加。

See TimeSpan from MSDN.

请参阅MSDN中的TimeSpan。

#3


 TimeSpan t1 = new TimeSpan(14, 20,0);
 TimeSpan t2 = new TimeSpan(0,30,0);
 Console.Out.WriteLine(t1 + t2);

#4


Try this (although 0:30pm doesn't make sense):

试试这个(虽然下午0:30没有意义):

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(new StringTime("14:20 pm").Add(new StringTime("0:30 pm")));
        Console.WriteLine(new StringTime("15:00 pm").Add(new StringTime("0:30 pm")));
        Console.WriteLine(new StringTime("5:00am").Add(new StringTime("12:00pm")));
    }
}

class StringTime
{
    public int Hours { get; set; }
    public int Minutes { get; set; }
    public bool IsAfternoon { get; set; }

    public StringTime(string timeString)
    {
        IsAfternoon = timeString.Contains("pm");
        timeString = timeString.Replace("pm", "").Replace("am", "").Trim();

        Hours = int.Parse(timeString.Split(':')[0]);
        Minutes = int.Parse(timeString.Split(':')[1]);
    }

    public TimeSpan ToTimeSpan()
    {
        if (IsAfternoon)
        {
            if (Hours < 12)
            {
                Hours += 12;
            }
        }
        return new TimeSpan(Hours, Minutes, 00);
    }

    public TimeSpan Add(StringTime time2)
    {
        return this.ToTimeSpan().Add(time2.ToTimeSpan());
    }
}

Output (the value before the dot are days):

输出(点前的值是天):

1.02:50:00
1.03:30:00
17:00:00

1.02:50:00 1.03:30:00 17:00:00

#5


You can't add those, just like you can't add "14:20PM" and the color red. You can add a time and a timespan (14:20PM + 30 minutes) or two timespans (2 hours+30 minutes). But you cannot add two times.

你不能添加那些,就像你不能添加“14:20 PM”和红色。您可以添加时间和时间跨度(下午14:20 + 30分钟)或两次时间(2小时+30分钟)。但你不能添加两次。

To make this even clearer, consider what would happen if you could add two times: 14.20 + 00:30 (EST) = 23.20 + 09:30 (UTC)

为了使这一点更加清晰,请考虑如果可以添加两次会发生什么:14.20 + 00:30(EST)= 23.20 + 09:30(UTC)

#6


This code enables a user to add times.

此代码使用户能够添加时间。

using System;
using System.Collections.Generic;

// ...
class Program
{
    static void Main(string[] args)
    {
        Console.ForegroundColor = ConsoleColor.Cyan;
        TimeCalculator time = new TimeCalculator();
        time.InsertTime(0, 0, 59);
        time.InsertTime(0, 0, 1);

        int[] totaltime = time.GetTime();

        Console.WriteLine($"Hours: {totaltime[0]}");
        Console.WriteLine($"Minutes: {totaltime[1]}");
        Console.WriteLine($"Seconds: {totaltime[2]}");

        Console.ForegroundColor = ConsoleColor.Gray;
        Console.Write("Press any key to quit . . . ");
        Console.ReadKey(true);
    }
}

class TimeCalculator
{
    int _hours;
    int _minutes;
    int _seconds;
    List<TimeCalculator> timelist = new List<TimeCalculator>();

    public void InsertTime(int hours, int minutes, int seconds)
    {
        timelist.Add(new TimeCalculator { _hours = hours, _minutes = minutes, _seconds = seconds });
        AddTime();
    }

    public void AddTime()
    {
        int sumHours = 0;
        int sumMinutes = 0;
        int sumSeconds = 0;

        foreach (var item in timelist)
        {
            sumHours += item._hours;
            sumMinutes += item._minutes;
            sumSeconds += item._seconds;
        }

        _hours = sumHours + (sumMinutes / 60);
        _minutes = (sumMinutes + (sumSeconds / 60)) % 60;
        _seconds = sumSeconds % 60;

        // % 60 restarts clock at 60
        // / 60 gives value at 60
    }

    public int[] GetTime()
    {
        return new int[] { _hours, _minutes, _seconds };
    }
}