如何才能只获得日期 [英] how can get only date

查看:82
本文介绍了如何才能只获得日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

com = new SqlCommand("SELECT MAX(createddate) FROM Payout_Master WHERE sponcorid='" + code + "'", con);
            DateTime date = (DateTime)com.ExecuteScalar();    
            if(date!=DateTime.Now.Date)
{

}


在这里,我只想比较日期,而不是时间.它需要datetime,所以我无法只比较日期...
例如:


here i want just to compare date, not the time., it takes datetime so i am unable to compare only date...
for example:

 if(11/21/2012 != 11/22/2012)
{
        //
}

推荐答案

使用如何仅比较日期中没有日期的日期C#中的DateTime类型? [
Use DateTime.Date Property[^] to extract the date component of a DateTime value.

Check this link
How to compare only Date without Time in DateTime types in C#?[^]


尝试:
DateTime date = (DateTime)com.ExecuteScalar();
if(date.Date != DateTime.Now.Date)
   {

DateTime.Date属性返回日期,并将时间部分设置为零:即午夜.如果在比较的两边都使用日期,则与没有时间的比较相同.

The DateTime.Date property returns the date, with the time part set to zero: I.e. Midnight. If you use Date on both sides of the comparison, it is the same as comparing without the time.




尝试使用以下代码进行补偿.
Hi,

Try the below code to compair.
if(dtOne.Date == dtTwo.Date)


请参考以下链接.

Date Compair [ ^ ]

http://social.msdn.microsoft.com /Forums/zh-CN/netfxbcl/thread/45b6164d-68db-4430-9342-cb2d266c94ff/ [


Refer the below link.

Date Compair[^]

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/45b6164d-68db-4430-9342-cb2d266c94ff/[^]


这篇关于如何才能只获得日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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