Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hello Friends
 
I want to convert integer value 1 to convert it into datetime and then subtract it with a datetime value.
I am using this code
DateTime lasttime = LastUpdateTime - Convert.ToDateTime(1);
where LastUpdateTime is DateTime type variable.
It is giving me an error
Cannot implicitly convert type 'System.TimeSpan' to 'System.DateTime'.
Can any one help.
 
Thanks in advance
Posted 26-Jan-12 23:36pm
Edited 26-Jan-12 23:44pm
v2
Comments
Anuja Pawar Indore at 27-Jan-12 5:45am
   
Added pre tag
BillWoodruff at 27-Jan-12 10:00am
   
In this type of question the intent of what you mean by "subtract it" is unclear: you leave it up to our imaginations to assume that you mean subtract one day, one hour, one ?
 
Please try to be very specific: tell us what the integer value #1 means in this question.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hello RashdSiddique
 
there seem to be confusion on DateTime and TimeSpan.
  1. DateTime is a specific point in time.
  2. A TimeSpan is a duration.
  3. The difference between two points in time (DateTime) is a duration (TimeSpan).
  4. You cannot make a duration being a point in time.
  5. You can add a duration to a point in time to get another specific point in time.
 
Use one of the several Add... methods of the DateTime class to accomplish the desired result.
 
Note that adding a negative amount equals to subtract that positive amount - that's why the DateTime class has no Subtract... methods.
 
Finally, the Convert.ToDateTime(int) seems very odd to me. As a maintainer I would dislike such code, since it is anti-intuitively and leaves a crucial question open: what unit?
 
Cheers
 
Andi
  Permalink  
v3
Comments
BillWoodruff at 27-Jan-12 9:58am
   
+5 Very nice comprehensive summary ! One very small suggestion: use "to get another specific point in time" in point #5.
Andreas Gieriet at 27-Jan-12 10:39am
   
Thanks for your 5 and your suggestion for improvement!
Cheers
Andi
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

use
LastUpdateTime.AddDays(-1);
  Permalink  
v2
Comments
Anuja Pawar Indore at 27-Jan-12 5:45am
   
Added pre tag
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

There are two problems there:
1) What do you expect to get as a date time combination from Convert.ToDateTime(1)? Because all you will get is the run time error "Invalid cast from 'Int32' to 'DateTime'."
"1" is not a date.
2) If you subtract two dates, you do not get a date - you get a time interval. "Tomorrow minus last Tuesday" will give you a number of days, not a date. Try using a TimeSpan to receive it instead.
 
What you are probably trying to do is work out the time interval since the last update:
TimeSpan ts = DateTime.Now - LastUpdateTime;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Convert.ToDateTime(integer) is not supported.
Have a look at this[^] documentation.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 450
1 OriginalGriff 403
2 Sergey Alexandrovich Kryukov 189
3 CPallini 189
4 CHill60 180
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 4,972
2 Maciej Los 3,269
3 Peter Leow 3,129
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 27 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100