S
sr
Anyone know of a better way to simulate a datediff for C#, i.e.,
without referencing the VB.NET runtime?
Only added the functionality that was needed for me so it is not
the full implementation of the DateDiff in the VB.NET runtime.
private int DateDiff( string Interval, DateTime Date1, DateTime Date2 )
{
int intDateDiff = 0;
TimeSpan time = Date1 - Date2;
int timeHours = Math.Abs( time.Hours );
int timeDays = Math.Abs( time.Days );
switch(Interval.ToLower())
{
case "h": // hours
intDateDiff = timeHours;
break;
case "d": // days
intDateDiff = timeDays;
break;
case "w": // weeks
intDateDiff = timeDays / 7;
break;
case "bw": // bi-weekly
intDateDiff = (timeDays / 7) / 2;
break;
case "m": // monthly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = timeDays / 30;
break;
case "bm": // bi-monthly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = (timeDays / 30) / 2;
break;
case "q": // quarterly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = (timeDays / 90);
break;
case "y": // yearly
intDateDiff = (timeDays / 365);
break;
}
return intDateDiff;
}
SR
without referencing the VB.NET runtime?
Only added the functionality that was needed for me so it is not
the full implementation of the DateDiff in the VB.NET runtime.
private int DateDiff( string Interval, DateTime Date1, DateTime Date2 )
{
int intDateDiff = 0;
TimeSpan time = Date1 - Date2;
int timeHours = Math.Abs( time.Hours );
int timeDays = Math.Abs( time.Days );
switch(Interval.ToLower())
{
case "h": // hours
intDateDiff = timeHours;
break;
case "d": // days
intDateDiff = timeDays;
break;
case "w": // weeks
intDateDiff = timeDays / 7;
break;
case "bw": // bi-weekly
intDateDiff = (timeDays / 7) / 2;
break;
case "m": // monthly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = timeDays / 30;
break;
case "bm": // bi-monthly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = (timeDays / 30) / 2;
break;
case "q": // quarterly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = (timeDays / 90);
break;
case "y": // yearly
intDateDiff = (timeDays / 365);
break;
}
return intDateDiff;
}
SR