日期代码有问题 [英] problem in date code

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

问题描述

我需要在c#中使用monthcalender将系统日期与所选日期的日期进行比较
我该如何编写代码
如果(monthCalendar1.SelectionRange.Start.ToString()= DateTime.Now);
什么是写代码到

i need to compare the date of system by the date of selected date in monthcalender in c#
how can i write the code
if (monthCalendar1.SelectionRange.Start.ToString()= DateTime.Now) ;
what is write code to

推荐答案

使用运算符比较日期(或更一般地,以System.DateTime形式的时间点:"==", >",<",> =和> =".此外,请使用-"(减法)查找时间间隔,持续时间.

Compare the dates (or, more generally, points of time in the form of System.DateTime using the operators: "==", ">", "<", ">=" and ">=". Also, use "-" (subtraction) to find the time interval, duration.

System.DateTime before = //...
System.DateTime after = //...

//...

if (before == after) { /* same time */ }
if (after >= before) { /* as expected */ }

//...

if (after < before) { /* something's wrong */ }

System.TimeSpan duration = after - before; // as simple as that! now see how much time elapsed



请参阅:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.timespan.aspx [ ^ ].

—SA



Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^].

—SA


不要执行ToString.你到底为什么会呢?字符串比较将无法正确比较日期. DateTime.Now现在.从一个小时起算,一个小时就更少.记住这一点.除此之外,如果您删除ToString,我敢打赌,您会得到一个DateTime,它将起作用.
Don''t do ToString. why on earth would you ? A string comparison will not correctly compare dates. DateTime.Now is NOW. An hour from how is greater, an hour less is less. Remember that. Apart from that, if you remove the ToString, I bet you''re getting a DateTime and this would work.


这篇关于日期代码有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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