如何将HHmm与今天当前时间的HHmm进行比较 [英] How to compare HHmm against HHmm of today current time

查看:256
本文介绍了如何将HHmm与今天当前时间的HHmm进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,该表具有int数据类型,可以保存代表HHmm的4位数字.我从数据库中获取了4位数字,并以这种普通格式1130返回给我,在11到30之间没有冒号,并且没有AM或PM.之后,我想从今天的if语句中的datetime组件中与HHmm进行比较,以下是代码.

我通过创建数据表从下表中提取了4位数字.

I have a table that has int datatype to save 4 digits that represent for HHmm. I fetch 4 digits from database, and returns to me in this plain format 1130, no colon between 11 and 30 and no AM or PM. After that, I want to compare with HHmm from today datetime''s components in ''if statement'', and below are codes.

I fetched 4digits from table as below by creating datatable.

int sTime = Convert.ToInt32(dr["SendTime"].ToString())



它返回为1130

然后,我今天打电话给datetime,并像这样格式化为HHmm.



It returns as 1130

Then, I called today datetime and formatted as HHmm like this.

DateTime TodayHrMin = DateTime.Now; //Get today's date and time
string todayHourMin = TodayHrMin.ToString("HHmm"); //Convert today's date and time to string format
int iHourMin = Convert.ToInt32(todayHourMin);//convert to int so that it can compare with int sTime



当我在if语句中比较这两个值(sTime和iHourMin)并执行某些操作时,它在if语句中未执行任何操作.有没有一种方法可以通过使用正确的日期和时间格式来解决此问题.几个月前,我开始学习asp .net,但在编写代码方面没有太多经验..谢谢.



When I compare these two values (sTime and iHourMin) in if statement and do some action, it is not doing any action in if statement. Is there a way to solve this by using proper date and time formatting. I started learning asp .net a few months ago and not so much experienced in writing codes.. Thank.

推荐答案

错误的方法.不要操纵表示数据的字符串,不要操纵数据.不要比较字符串,不要比较表示时间的整数,不要比较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


这篇关于如何将HHmm与今天当前时间的HHmm进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆