开发者

Date Comparing Solutions

Using C# & MySQL

Table1

ID Date1 Date2

001 04/05/2010 05/06/2010
002 04/06/2010 07/08/2010
....,

to Date2 Datatype is Varchar, Format: mm/dd/yyyy

The Date a开发者_如何学运维lways compare with system date, when the date is going to expire, it will show the message for i 1 week like this..

7 days to expire
6 day to expire
5 day to expire
...
...
1 day to expire.


Don't use strings for dates. It seems like a logical solution to localization issues, but it will always be way more hassle than it's worth.

That said, if you're set on that decision, you can use DateTime.Parse with an IFormat provider to parse youre specific implementation of the date:

DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
dtFormat.DateSeparator = "/";
dtFormat.TimeSeparator = ":";
dtFormat.ShortDatePattern = "MM/dd/yyyy";
dtFormat.ShortTimePattern = "HH:mm:ss";
return dtFormat;

Once you have the date (and again, it would be easier to just get this from the database without parsing/doing comparisons in code) you can calculate a timespan in C# by:

DateTime.Now.Subtract(myDate).TotalDays

The Subtract function returns a TimeSpan.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜