p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

csharptoday_discuss thread: Subtract two dates


Message #1 by goodr@m... on Tue, 21 May 2002 17:04:41
I am trying to subtract two dates from each other using 
the System.DateTime.Subtract() method but i continuly get 
an error of Cannot implicitly convert 
type 'System.TimeSpan' to 'System.DateTime' every time i 
compile.
The code looks like this

using data reader to pull the new shift date

shiftDate = dr["first_date_new_shift"].ToString();

DateTime dt = DateTime.Now;

if (dt.Hour < 9 | dt.Hour > 23) dt = dt.AddDays(-1)

error occurs on line below
dt = dt.Subtract(DateTime.Parse(shiftDate).AddDays(1));

The object browser shows a Subtract.TimeSpan but if have 
clearly set "dt" to System.DateTime so i can't see were 
the error is coming from.

Thanks,
Ron
Message #2 by "Steve Barta" <steve.barta@t...> on Fri, 24 May 2002 23:12:55
Have you tried just subtracting the 2 objects directly?
dt = dt - shiftDate.AddDays(1);

> I am trying to subtract two dates from each other using 
t> he System.DateTime.Subtract() method but i continuly get 
a> n error of Cannot implicitly convert 
t> ype 'System.TimeSpan' to 'System.DateTime' every time i 
c> ompile.
T> he code looks like this

> using data reader to pull the new shift date

> shiftDate = dr["first_date_new_shift"].ToString();

> DateTime dt = DateTime.Now;

> if (dt.Hour < 9 | dt.Hour > 23) dt = dt.AddDays(-1)

> error occurs on line below
d> t = dt.Subtract(DateTime.Parse(shiftDate).AddDays(1));

> The object browser shows a Subtract.TimeSpan but if have 
c> learly set "dt" to System.DateTime so i can't see were 
t> he error is coming from.

> Thanks,
R> on

  Return to Index