C#如何计算即将到来的具体时间?

C#如何计算即将到来的具体时间?,c#,forex,C#,Forex,我正在创建一个应用程序,它告诉我外汇市场还有多少时间打开或关闭 例如,如果纽约市场刚刚接近,现在我想知道纽约市场还有多少时间再次开盘。还有一些是在开门的时候关门的 我可以用定时器来减少剩余时间,但问题是我不能得到一个绝对减去的剩余时间值。即使使用TimeSpan.Subtract或DateTime.Subtract也不行 编辑:我用这个代码告诉我市场是开放的 if ((IsTimeOfDayBetween(DateTime.UtcNow, new TimeSpan(8, 0, 0), new

我正在创建一个应用程序,它告诉我外汇市场还有多少时间打开或关闭

例如,如果纽约市场刚刚接近,现在我想知道纽约市场还有多少时间再次开盘。还有一些是在开门的时候关门的

我可以用定时器来减少剩余时间,但问题是我不能得到一个绝对减去的剩余时间值。即使使用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;
if(open.CompareTo(close)>0)//如果打开时间更长(例如打开:(20,0,0)关闭:(4,0,0))
{

如果(TimeNo.TimeNo.CopReto(open)>=0×TimeNo.Fayto(CHILL)= 0和& TimeNo.TimeDay.