有时间到下午6点发生?

时间:2017-03-10 17:44:43

标签: c# wpf

我有一个线程只能在一定时间内运行,否则会延迟。从运行某个布尔值的线程的时间开始,如果为true,那么它应该从当前时间延迟X时间到18:00。在c#中有快速的方法吗?

2 个答案:

答案 0 :(得分:1)

DateTime today = DateTime.Now; 

        DateTime tomorrow = today.Add(new TimeSpan(1,0,0,0));

        DateTime tomorrowAtSix = new DateTime(tomorrow.Year, tomorrow.Month, tomorrow.Day, 18,0,0 ); 

        TimeSpan diff = tomorrowAtSix.Subtract(DateTime.Now); 

        double hoursFromNow = 0d; 
        double minutesFromNow = 0d; 
        if(diff.TotalHours > 24d)
        {
             hoursFromNow = diff.TotalHours - 24d; 
             minutesFromNow = diff.TotalMinutes - (24d * 60d); 
        }
        else 
        {
             hoursFromNow = diff.TotalHours;
             minutesFromNow = diff.TotalMinutes; 
        }

答案 1 :(得分:0)

您可以减去将返回TimeSpan的DateTime对象

require 'json'
# => true 
sym_hash = {:id=>58, :locale=>:"en-US"}
# => {:id=>58, :locale=>:"en-US"} 
string_hash = {"id"=>58, "locale"=>"en-US"}
# => {"id"=>58, "locale"=>"en-US"} 
sym_hash.to_json == string_hash.to_json
# => true