C#如何计算即将到来的具体时间?
C# How to calculate upcoming specific time?
我正在创建一个应用程序,它告诉我外汇市场开盘或收盘还剩多少时间。
例如,如果纽约市场刚刚收盘,现在我想知道纽约市场还剩多少时间再次开市。有些人在它开放时关闭它的时间。
我可以使用计时器来减少剩余时间,但问题是我无法获得绝对减去的剩余时间值。甚至 TimeSpan.Subtract 或 DateTime.Subtract 也不行。
编辑:我使用此代码告诉我市场开放
if ((IsTimeOfDayBetween(DateTime.UtcNow, new TimeSpan(8, 0, 0), new TimeSpan(16, 0, 0))) == true)
{
textBox1.Background = new SolidColorBrush(Colors.Green);
}
但是在它关闭后,我希望它还有剩余时间再次打开,显示在文本块中。
请尝试以下代码。希望对您的问题有所帮助
var dt = new DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime();
var now = System.DateTime.Now.ToUniversalTime();
var future = new DateTime(2010, 1, 1).ToUniversalTime();
Console.WriteLine((now - dt).TotalSeconds);
Console.WriteLine((future - dt).TotalSeconds);
您可以尝试这样的操作,使用您已有的函数 "IsTimeOfDayBetween":
// global variables
TimeSpan dayStart = new TimeSpan(0, 0, 0);
TimeSpan dayEnds = new TimeSpan(23, 59, 59);
// you have a timer(loop) every sencond, so you can have this two variables change depending on what market you are tracking, in this example NY
TimeSpan NyOpens = new TimeSpan(8, 0, 0);
TimeSpan NyClose = new TimeSpan(16, 0, 0);
// variable to store the result
TimeSpan tillOpenClose;
if ((IsTimeOfDayBetween(DateTime.UtcNow, NyOpens, NyClose)) == true)
{
textBox1.Background = new SolidColorBrush(Colors.Green);
// its open, you want time till close
// validate close time is greater than open time
if ( NyClose.CompareTo(NyOpens) > 0)
tillOpenClose = NyClose.Subtract(DateTime.UtcNow.TimeOfDay);
else
tillOpenClose = ((dayEnds.Subtract(DateTime.UtcNow.AddSeconds(-1).TimeOfDay)).Add(NyClose)); // if the market closes at and earlier time, then the time till close, is the remaing time of this day, plus the time till close of the new day
}
else if ((IsTimeOfDayBetween(DateTime.UtcNow, dayStart, NyOpens)) == true) // if time is between start of day and open time
tillOpenClose = NyOpens.Subtract(DateTime.UtcNow.TimeOfDay);
else // it is between closetime and end of day
tillOpenClose = ((dayEnds.Subtract(DateTime.UtcNow.AddSeconds(-1).TimeOfDay)).Add(NyOpens)); // part remaining for this day plus new day, the extra second is to compensate the "dayEnds"
Console.WriteLine(tillOpenClose.ToString(@"hh\:mm\:ss"));
而 "IsTimeOfDayBetween" 函数应该是这样的:
if (open.CompareTo(close) > 0) // if open time is greater (e.g. open: (20,0,0) close: (4,0,0))
{
if (timeNow.TimeOfDay.CompareTo(open) >= 0 || timeNow.TimeOfDay.CompareTo(close) <= 0)
return true;
}
else
{
if (timeNow.TimeOfDay.CompareTo(open) >= 0 && timeNow.TimeOfDay.CompareTo(close) <= 0)
return true;
}
return false;
编辑:将时间更改为关闭,抱歉
EDIT2:调整关闭时间早于开放时间
我正在创建一个应用程序,它告诉我外汇市场开盘或收盘还剩多少时间。
例如,如果纽约市场刚刚收盘,现在我想知道纽约市场还剩多少时间再次开市。有些人在它开放时关闭它的时间。
我可以使用计时器来减少剩余时间,但问题是我无法获得绝对减去的剩余时间值。甚至 TimeSpan.Subtract 或 DateTime.Subtract 也不行。
编辑:我使用此代码告诉我市场开放
if ((IsTimeOfDayBetween(DateTime.UtcNow, new TimeSpan(8, 0, 0), new TimeSpan(16, 0, 0))) == true)
{
textBox1.Background = new SolidColorBrush(Colors.Green);
}
但是在它关闭后,我希望它还有剩余时间再次打开,显示在文本块中。
请尝试以下代码。希望对您的问题有所帮助
var dt = new DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime();
var now = System.DateTime.Now.ToUniversalTime();
var future = new DateTime(2010, 1, 1).ToUniversalTime();
Console.WriteLine((now - dt).TotalSeconds);
Console.WriteLine((future - dt).TotalSeconds);
您可以尝试这样的操作,使用您已有的函数 "IsTimeOfDayBetween":
// global variables
TimeSpan dayStart = new TimeSpan(0, 0, 0);
TimeSpan dayEnds = new TimeSpan(23, 59, 59);
// you have a timer(loop) every sencond, so you can have this two variables change depending on what market you are tracking, in this example NY
TimeSpan NyOpens = new TimeSpan(8, 0, 0);
TimeSpan NyClose = new TimeSpan(16, 0, 0);
// variable to store the result
TimeSpan tillOpenClose;
if ((IsTimeOfDayBetween(DateTime.UtcNow, NyOpens, NyClose)) == true)
{
textBox1.Background = new SolidColorBrush(Colors.Green);
// its open, you want time till close
// validate close time is greater than open time
if ( NyClose.CompareTo(NyOpens) > 0)
tillOpenClose = NyClose.Subtract(DateTime.UtcNow.TimeOfDay);
else
tillOpenClose = ((dayEnds.Subtract(DateTime.UtcNow.AddSeconds(-1).TimeOfDay)).Add(NyClose)); // if the market closes at and earlier time, then the time till close, is the remaing time of this day, plus the time till close of the new day
}
else if ((IsTimeOfDayBetween(DateTime.UtcNow, dayStart, NyOpens)) == true) // if time is between start of day and open time
tillOpenClose = NyOpens.Subtract(DateTime.UtcNow.TimeOfDay);
else // it is between closetime and end of day
tillOpenClose = ((dayEnds.Subtract(DateTime.UtcNow.AddSeconds(-1).TimeOfDay)).Add(NyOpens)); // part remaining for this day plus new day, the extra second is to compensate the "dayEnds"
Console.WriteLine(tillOpenClose.ToString(@"hh\:mm\:ss"));
而 "IsTimeOfDayBetween" 函数应该是这样的:
if (open.CompareTo(close) > 0) // if open time is greater (e.g. open: (20,0,0) close: (4,0,0))
{
if (timeNow.TimeOfDay.CompareTo(open) >= 0 || timeNow.TimeOfDay.CompareTo(close) <= 0)
return true;
}
else
{
if (timeNow.TimeOfDay.CompareTo(open) >= 0 && timeNow.TimeOfDay.CompareTo(close) <= 0)
return true;
}
return false;
编辑:将时间更改为关闭,抱歉
EDIT2:调整关闭时间早于开放时间