Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
int year = DateTime.Now.Year;
                  int month = DateTime.Now.Month;
                  int days = DateTime.Now.Day;

                  DateTime StartDate = new DateTime(year,month,days);
                  DateTime EndDate = StartDate.AddDays(7);


i am adding 7 days to datetime now.
its gets when debbuging from 03-24 until 03-31 but it should be 04-01?

why?
Posted
Comments
BillWoodruff 24-Mar-15 5:12am    
You are starting at the beginning of Day 24 (12 AM), and going until the beginning of Day 31 (12 AM). Count the days.

if you look at the calendar, next week tuesday is 31 march. not 1st of april.
 
Share this answer
 
You missed something here...
First! Why re-create Now instead of using it....
C#
DateTime StartDate = DateTime.Now;

Second! Let count together...
24 + 1 = 25
25 + 1 = 26
26 + 1 = 27
27 + 1 = 28
28 + 1 = 29
29 + 1 = 30
30 + 1 = 31

Now that's exactly! 7 lines!!!
 
Share this answer
 
Um.
M   T   W   Th  Fr  Sa  Su
23  24  25  26  27  28  29
30  31  01  02  03  04  05
24th plus 7 days == Tuesday 31st March...
 
Share this answer
 
Comments
Kurac1 24-Mar-15 5:12am    
yes but i want i to be from 24 to 1 april always 7 days
Kornfeld Eliyahu Peter 24-Mar-15 5:16am    
There is 7 days in-between the 1st of April and the 24th of March - it's true, but AddDays - of any system I know of - works in an other way...
You get the 7th day from the base day and not a gap of 7 days...If you want to get a gap of 7 days you have to add 8 days...But this is a special requirement and AddDays works just perfectly...
Kurac1 24-Mar-15 5:18am    
Alright because right now i add 7 days and it displays 6 days only
Kurac1 24-Mar-15 5:37am    
adding(8) still displays 6 days? why?
Yesterday i tried to add 8 days that it showed 7 days but today its tuesday and adding 8 days display 6 days still
OriginalGriff 24-Mar-15 5:52am    
Well...when I try your code, it works fine for me:
int year = DateTime.Now.Year;
int month = DateTime.Now.Month;
int days = DateTime.Now.Day;

DateTime StartDate = new DateTime(year, month, days);
DateTime EndDate = StartDate.AddDays(7);
DateTime Enddate2 = StartDate.AddDays(8);

I get 31/03/2015 and 01/04/2015. So what are we doing that is different?

BTW: Don't do it like that - that is a good source of very annoying and hard to find bugs! Think about it: what happens if the clock goes over midnight while executing that code?

Do this instead:

DateTime StartDate = DateTime.Now.Date;

It has the same effect, but it doesn't risk date rollover bugs.

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