Sto convertendo una piccola applicazione MSAccess in un'app ASP.NET basata sul Web, utilizzando C# 3.5. Mi stavo chiedendo quale sia il modo migliore per lavorare con le date in C#, quando si converte parte di questo codice VBA in C#.In C#, come si converte il tipo di dati TimeSpan in DateTime?
Ecco un esempio del codice VBA:
Coverage1=IIf(IsNull([EffDate1]),0,IIf([CurrDate]<=[EndDate1],[CurrDate]-[EffDate1],[EndDate1]-[EffDate1]+1))
Ecco quello che il mio attuale codice C# sembra che con gli errori indicati nel codice commentato:
public DateTime CalculateCoverageOne(DateTime dateEffDateOne, DateTime dateCurrentDate, DateTime dateEndDateOne)
{
if (dateCurrentDate.Date <= dateEndDateOne.Date)
{
return null; //Get "cannot convert null to System.DateTime because it is a non-nullable value type" error
}
else
{
if (dateCurrentDate.Date <= dateEndDateOne)
{
return dateCurrentDate.Subtract(dateEffDateOne); //Gets error "cannot implicitly convert system.timepsan to system.datetime
}
else
{
return dateEndDateOne.Subtract(dateEffDateOne.AddDays(1)); //Gets error "cannot implicitly convert system.timepsan to system.datetime
}
}
}
Più eccellente. Grazie mille! – program247365