C#_计算目前时间到指定的周X、指定的时间X 还有多少秒

时间:2021-09-05 21:19:04

比如:当前时间到下周二 05:00:00还剩下多少秒?

 /// <summary>
/// 计算距离下一个 周XX XX时XX分XX秒,还剩下多少秒
/// </summary>
/// <param name="currDateTime">当时间</param>
/// <param name="dayWeek">目标星期</param>
/// <param name="str_HourMinuteSecond">目标时间,时分秒的字符串格式 "05:00:00"</param>
/// <param name="totalSeconds">计算结果:还剩下的总秒数</param>
void CalEndTimeSeconds(DateTime currDateTime, DayOfWeek dayWeek, string str_HourMinuteSecond, out int totalSeconds)
{
totalSeconds = ;
TimeSpan tp = new TimeSpan();
if (TimeSpan.TryParse(str_HourMinuteSecond, out tp))
{
int endPointSeconds = (int)tp.TotalSeconds; int currDayPassSeconds = (int)currDateTime.TimeOfDay.TotalSeconds; //当天还剩下的秒数
totalSeconds = currDayPassSeconds <= endPointSeconds ? (endPointSeconds - currDayPassSeconds) : (perDaySeconds - currDayPassSeconds); if (currDateTime.DayOfWeek > dayWeek)
{
int days = - (int)currDateTime.DayOfWeek;
totalSeconds += days * perDaySeconds;
totalSeconds += endPointSeconds;
}
else if (currDateTime.DayOfWeek == dayWeek)
{
totalSeconds += ;
}
else
{
int days = (int)(dayWeek - currDateTime.DayOfWeek) - ;
totalSeconds += days * perDaySeconds;
totalSeconds += endPointSeconds;
}
}
}