开发者

convert vb date comparison to c#

I have a code in vb, and I'm trying convert it to c#.

_nextContactDate.ToShortDateString > New Date(1900, 1, 1)

This is _nexContractDate declaration:

   Private _nextConta开发者_Python百科ctDate As DateTime

It's weird for me. Comapre datetime to string?


What this code is doing is extracting the date part (i.e. removing the time part) and using VB's loose nature to allow a date represented as a string to be implicitly converted back to a date for the purposes of comparison with an actual date.

The correct way to remove the time part would be to check as follows:

_nextContactDate.Date > new DateTime(1900, 1, 1)

It seems odd, as this means that the 1st Jan 1900 will fail this check, and only dates from the 2nd Jan 1900 will pass. As such, I'd be inclined to check whether this code has a logic error.


I'm not sure I understand your question entirely, but why compare a DateTime to a string anyway, why not just compare dates?

if (_nextContactDate > new DateTime(1900, 1, 1))
{

}

As noted by Greg, currently the ToShortDateString removes some parts of the date (specifically, the time units), but upon comparison with a minimum date as such, this is rather redundant - if you are concerned at such a level, then you can compare only the Date members.


no, you don't need to compare DataTime variable in string format. you can compare like below:

DateTime myDate = new DateTime(2011,8,24);

if(myDate > DateTime.MinValue)
    DoSomething();


In many databases, time is stored as the minimum date + the time value.

So assuming the minimum date is 31 Dec 1899 2359H (if I reckon right, that's the minimum for Access) then 1300H will be stored as 01 Jan 1900 1300H.
Dates are stored as 'usual'. And dates with time components have the date value with the time component attached to them.

What's this got to do with the code? The original programmer is trying to determine whether the field is holding a date or a time value. The analogy is simple. If the value is time only, then by stripping off the time component, you'll be left with 01 Jan 1900. If it contains a date component, it's going to be more than 01 Jan 1900.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜