我想添加時間到當前時間或換句話說,我有時間在UTC + 0,我希望那個時間在UTC + 5.00, 我可以減去多少小時我可以C# - 無法添加小時到當前時間
- 它工作得很好 「(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();
}
}
}
你爲什麼要使用字符串操作? –
datetimevariable.AddHours(5);如果你需要減去AddHours(-5); – terrybozzio