判断某个时间是否在某一时间段内
string _strWorkingDayAM = "08:00" ;
string _strWorkingDayPM = "17:00" ;
TimeSpan dspWorkingDayAM = DateTime. Parse ( _strWorkingDayAM) . TimeOfDay;
TimeSpan dspWorkingDayPM = DateTime. Parse ( _strWorkingDayPM) . TimeOfDay;
string time1 = "2021-02-17 15:10:00" ;
TimeSpan dspNow = time1. TimeOfDay;
if ( dspNow > dspWorkingDayAM && dspNow < dspWorkingDayPM)
{
MessageBox. Show ( "在上班时间段!" ) ;
}
else
{
MessageBox. Show ( "不在上班时间段!" ) ;
}
TimeSpan t = dspWorkingDayPM - dspNow ;
double h = t. TotalHours;
double ds = t. TotalDays;
int D = t. Days;
计算两个dateTime的时间差
DateTime minDate = "2021/02/02 12:20:31" ;
DateTime maxDate = "2021/02/03 13:23:42" ;
TimeSpan d3 = maxDate. Subtract ( minDate) ;
String a = "相差:" + d3. Days. ToString ( ) + "天" + d3. Hours. ToString ( ) + "小时" + d3. Minutes. ToString ( ) + "分钟" + d3. Seconds. ToString ( ) + "秒" ;
获取时间的小时部分
int h = DateTime. Now. Hour;
int m = DateTime. Now. Minute;
int s = DateTime. Now. Second;
DateTime t = DateTime. Now;
string t1 = DateTime. Now. ToString ( ) ;