Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying that code.

C#
dt.Subtract(new TimeSpan(totCount)).ToString();


but it subtract only one day where as count is 1,2,3,4,5,6..etc

and its gives a result like-

12/16/2011 11:59:59 PM but i want result like Wednesday, December 14, 2011

Please help me out.
Posted
Updated 27-Dec-11 1:01am
v2
Comments
Karthik Harve 27-Dec-11 7:02am    
[Edit] pre tags added.
Supriya Srivastav 27-Dec-11 7:02am    
what's dt here?
mayankshrivastava 27-Dec-11 7:03am    
date time
Richard MacCutchan 27-Dec-11 7:04am    
Why are you trying to subtract a string from a DateTime?
mayankshrivastava 27-Dec-11 7:06am    
m trying to subtract selected date from integer
my code is-
DateTime dt = new DateTime();
dt = Calendar.SelectedDate;
dt = dt.AddDays(Convert.ToInt32(txtadd.Text));
lblgetdate.Text = dt.ToLongDateString();
DateTime sDate = new DateTime();
sDate = Calendar.SelectedDate;
DateTime eDate = new DateTime();
eDate = Convert.ToDateTime(lblgetdate.Text);
DayOfWeek fWeek = DayOfWeek.Friday;
int totCount = findWeekCount(sDate, eDate, fWeek);
lblweekday.Text = totCount.ToString();
lbldate.Text = dt.Subtract(new TimeSpan(totCount)).ToString();

The problem is that you are using the wrong TimeSpan constructor: the Int64 version, which constructs a TimeSpan based on a number of ticks.
If you want to subtract a number of days, then do not construct a timespan, use the DateTime.AddDays method with a negative value parameter instead:
C#
int totalCount = 6;
DateTime dt = DateTime.Now;
dt = dt.AddDays(-totalCount);
 
Share this answer
 
Comments
mayankshrivastava 27-Dec-11 7:14am    
thanks ....
OriginalGriff 27-Dec-11 7:54am    
You're welcome!
You Can try
DateTime date = (DateTime.Now).AddDays(-2);


You can replace DateTime.Now with your date and 2 with integer value.
 
Share this answer
 
v2
Comments
mayankshrivastava 27-Dec-11 7:16am    
thaks supriya
Supriya Srivastav 27-Dec-11 7:37am    
Welcome Dear....
mayankshrivastava 27-Dec-11 7:39am    
My Pleasure...
RaviRanjanKr 27-Dec-11 17:12pm    
[Edited]Code is wrapped in pre tag[/Edited]
DateTime.AddDays[^] can take a negative number.

If your starting date is 12/16/2011 then

C#
dt.AddDays(-2);


will be 12/14/2001
 
Share this answer
 
int totalCount = 6;
            DateTime dt = DateTime.Now;
            dt = dt.AddDays(-totalCount);
 
Share this answer
 
v2
Comments
RaviRanjanKr 27-Dec-11 17:26pm    
[Edited]Code is wrapped in pre tag[/Edited]

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900