C# - 不能在当前时间增加小时数

时间:2013-11-01 23:54:31

标签: c#

我想为当前时间增加时间,换句话说,我有时间在UTC + 0,我想要UTC + 5.00的时间, 我可以减去尽可能多的小时

  • 它适用于“(20:15 current_time) - (10)”
  • 它适用于“(20:12 current_time) - (10 * 324234小时)”
  • 即使“(20:13 current_time)+(1)”添加
  • 也能正常工作
  • 但是当“(20:13 current_time)+(5小时)”
  • 时它不起作用

代码:

using System;

namespace week3
{

class LocalTime
{
    static string Time, City;
    static decimal time;

    public LocalTime(string city, double add) {
        Time = Convert.ToString(DateTime.Now + TimeSpan.FromHours(add));
        Time = Time.Substring(11, 5);
        time = Convert.ToDecimal(Time.Substring(0, 2) + "." + Time.Substring(3, 2));
        City = time.ToString();
        Time = City.Substring(0, 2) + ":" + City.Substring(3, 2);
        City = city;
        DisplayTimeAndCity("", "");

    }
    static void DisplayTimeAndCity(string x, string y)
    {

        Console.WriteLine(City + " - " +Time);
    }
}

class London : LocalTime {
    public London() : base("London", 0) {   
    }
}
class NewYork : LocalTime
{
    public NewYork(): base("NewYork", 5)
    {
    }
}
class Tokyo : LocalTime
{
    public Tokyo(): base("Tokyo", -9)
    {
    }
}
class HongKong : LocalTime
{
    public HongKong(): base("Hong Kong", -8)
    {
    }
}
class Test {
    static void Main() {
        London a = new London();
        NewYork b = new NewYork();

    }

}

}

4 个答案:

答案 0 :(得分:1)

要向DateTime添加5个小时,您只需使用以下内容:

DateTime.Now.AddHours(5);

还有一个简单的函数可以从DateTime中减去:

DateTime.Now.Subtract(TimeSpan.FromHours(1));

答案 1 :(得分:1)

当您尝试在不同时区表示时间时,您可能会发现TimeZoneInfo.ConvertTimeBySystemTimeZoneId有用。您可能更愿意使用框架中的某些内容,而不是定义多个类。标识每个区域的字符串文档位于TimeZoneInfo.FindSystemTimeZoneById Method,字符串与TimeZone.Id property相关。我在Windows 8.1上测试了它,它工作正常。

  

时区标识符是唯一标识a的密钥字符串   特定时区。在Windows XP和Windows Vista中,它对应   到HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows的子项   注册表的NT \ CurrentVersion \ Time Zone分支。它可以通过   作为FindSystemTimeZoneById方法的参数来检索   注册表中的特定时区。

void Main()
{
    DateTime currentTime = DateTime.Now;
    Console.WriteLine("Current Times:");
    Console.WriteLine();
    Console.WriteLine("Los Angeles: {0}", 
                      TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Pacific Standard Time"));
    Console.WriteLine("Chicago: {0}", 
                      TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Central Standard Time"));
    Console.WriteLine("New York: {0}", 
                      TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Eastern Standard Time"));
    Console.WriteLine("London: {0}", 
                      TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "GMT Standard Time"));
    Console.WriteLine("Moscow: {0}", 
                      TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Russian Standard Time"));
    Console.WriteLine("New Delhi: {0}", 
                      TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "India Standard Time"));
    Console.WriteLine("Beijing: {0}", 
                      TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "China Standard Time"));
    Console.WriteLine("Tokyo: {0}", 
                      TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Tokyo Standard Time"));
}

列出了本地计算机上可以找到的时区:

ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();
Console.WriteLine("The local system has the following {0} time zones", zones.Count);
foreach (TimeZoneInfo zone in zones)
   Console.WriteLine(zone.Id);

答案 2 :(得分:0)

您也可以将DateTimeOffset用于您的目的:

DateTimeOffset dateTimeWithOffset = new DateTimeOffset(DateTime.UtcNow).ToOffset(TimeSpan.FromHours(5));

这样,您将以标准方式保留时间和偏移信息。

答案 3 :(得分:0)

我认为您要找的是“如何将DateTime值格式化为hours:minutes”:

 var time = (DateTime.Now + TimeSpan.FromHours(3)).ToString("hh:mm");   

有关格式化选项的更多信息 - Custom Date and Time Format Strings

请注意,其他答案提供了更好的方法来通过TimeZoneInfoDateTimeOffset来处理时区。