Click here to Skip to main content
12,625,698 members (38,522 online)
Rate this:
 
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 27-Jan-12 0:36am
Updated 27-Jan-12 0:44am
v2
Comments
Anuja Pawar Indore 27-Jan-12 5:45am
   
Added pre tag
BillWoodruff 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 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 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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 27 Jan 2012
Copyright © CodeProject, 1999-2016
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