且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将HHmm与今天当前时间的HHmm进行比较

更新时间:2023-01-29 08:14:50

错误的方法.不要操纵表示数据的字符串,不要操纵数据.不要比较字符串,不要比较表示时间的整数,不要比较System.DateTime类型的对象.尝试避免使用字符串,但是如果某些数据来自字符串,请先对其进行解析,然后再进行比较.
请查看所有名为ParseParseExactTryParseTryParseExact的方法:
http://msdn.microsoft.com/en-us/library/system.datetime%28v = vs.110%29.aspx [
Wrong approach. Don''t manipulate strings representing data, manipulate data. Don''t compare strings, don''t compare integers representing time, compare the objects of System.DateTime type. Try to avoid using strings, but if some data comes from strings, parse it first, and then compare.
Please see all the methods named Parse, ParseExact, TryParse and TryParseExact:
http://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx[^].

For comparison, use the operators defined for this type: ''=='', ''>='', ''<='', ''<'', ''>'', ''-''.

—SA


DateTime具有获取其小时和分钟值的属性.

为什么不尝试呢?

DateTime has properties to get its Hour and Minute values.

Why don''t you try this?

int DBTime = 1130;


//Parse DB Hour and Minute
int hour = DBTime / 100;
int minute = DBTime % 100;

DateTime TodayHrMin = DateTime.Now; //Get today's date and time
int todayHour = TodayHrMin.Hour; //today's Hour
int todayMinute = TodayHrMin.Minute; //today's Minute

//TODO: Your Logic



或者您也可以通过其他方式做到:



or you can do it in the other way:

DateTime TodayHrMin = DateTime.Now; //Get today's date and time

int todayHour = TodayHrMin.Hour; //today's Hour
int todayMinute = TodayHrMin.Minute; //today's Minute

//Parse DB Hour and Minute
int TodayDBFormattedTime = (todayHour * 100) + todayMinute;

//TODO: Your Logic