понедельник, 19 января 2009 г.

Date difference in C#

There is a very useful function for quantative comparing of dates in VisualBasic - DateDiff. This is a cause of a tiny envy of a numerous C#-coders, because there aren't an analog in C#. Well, in C# we could make a little workaround:
C#:
TimeSpan dateDiff(DateTime firstDate,DateTime secondDate)
{
 return secondDate - firstDate;
}
Then, just take a necessary time unit (from days to milliseconds) from the Timespan structure:
C#:
TimeSpan ts = dateDiff(today, DateTime.Parse('1999-01-01'));
int days=ts.Days;
int minutes=ts.Minutes;

Комментариев нет: