Incrimenting Dates in C#

D

Dogmar Hoffman

Hello,

I am pretty new to C# and have been surfing this forum. It has been a
great help. I am hoping that you can help with this issue:

I have the following:

TimeSpan Diff = new TimeSpan();
Diff = m_EndTime.Subtract(m_StartTime);
//make array to store all the dates to print
for(int j = Diff.Days;j>0;j--)
{
//change to read array in a loop and make each file
FileName = FileDirectory + "\\" + this.m_StartTime.ToString("yyyyMMdd")
+ j + FileIndx.ToString() + ".txt";
}

Basically, I would need to incriment the m_StartTime j times. Then I
need to store them in an array and run through the dates there to create
a filename for each one. Unless there is a better way. The end result
should just be like this:

m_StartDate = 20060303
m_EndDate = 20060306
j = 4

int [] DateArray contains:
[20060303,20060304,20060305,20060306]

and filenames created are:

20060303.txt
20060304.txt
20060305.txt
20060306.txt

If you could help with this, I would greatly appreciate it.

DH
 
M

Martin Schall

Dogmar said:
Hello,

I am pretty new to C# and have been surfing this forum. It has been a
great help. I am hoping that you can help with this issue:

I have the following:

TimeSpan Diff = new TimeSpan();
Diff = m_EndTime.Subtract(m_StartTime);
//make array to store all the dates to print
for(int j = Diff.Days;j>0;j--)
{
//change to read array in a loop and make each file
FileName = FileDirectory + "\\" + this.m_StartTime.ToString("yyyyMMdd")
+ j + FileIndx.ToString() + ".txt";
}

Basically, I would need to incriment the m_StartTime j times. Then I
need to store them in an array and run through the dates there to create
a filename for each one. Unless there is a better way. The end result
should just be like this:

m_StartDate = 20060303
m_EndDate = 20060306
j = 4

int [] DateArray contains:
[20060303,20060304,20060305,20060306]

and filenames created are:

20060303.txt
20060304.txt
20060305.txt
20060306.txt

If you could help with this, I would greatly appreciate it.

DH

Hello,
DateTime has public methods for addition. Maybe you could use the method
'AddDays' for your problem.

Martin
 
D

Dogmar Hoffman

Thank you for the response. So I am using that now, but could this
work?

TimeSpan Diff = new TimeSpan();
Diff = m_EndTime.Subtract(m_StartTime);
double j = Diff.Days;
for(int i =0;i<j;i++)
{
this.m_StartTime = this.m_StartTime.AddDays(i);
FileName = FileDirectory + "\\" + this.m_StartTime.ToString("yyyyMMdd")
+ j + FileIndx.ToString() + ".txt";
}

Right now, it is just creating 1 file with the original m_StartTime as
the title. (so it is adding i when i = 0) I would like it to keep
creating files for each date between m_StartDate and m_EndDate. I just
can not figure out why it will only go through the loop 1 time. Thank
you very much!
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Some problems with your code:

You do not need to instantiate Diff
TimeSpan Diff = new TimeSpan();
Diff = m_EndTime.Subtract(m_StartTime);

TimeSpan.Days is integer, why use a double?
double j = Diff.Days;
for(int i =0;i<j;i++)
{
// no need for this line, not only that but it;s wrong !
this.m_StartTime = this.m_StartTime.AddDays(i);

//I changed this line, included: this.StartTime.AddDays( i)
FileName = FileDirectory + "\\" + this.m_StartTime.AddDays(
i).ToString("yyyyMMdd")
+ j + FileIndx.ToString() + ".txt";
}

It should work now.


Try it
 
D

Dogmar Hoffman

Thank you very much.

This is a bit confusing. Here is what it does:

m_StartDate = 20060309
m_EndDate = 20060313

Only one file named 20060312.txt gets created...

The code as it stands is:

TimeSpan Diff = m_EndTime.Subtract(m_StartTime);
int j = Diff.Days;
for(int i =0;i<j;i++)
{
FileName = FileDirectory + "\\" +
this.m_StartTime.AddDays(i).ToString("yyyyMMdd")+ "_" +
FileIndx.ToString() + ".txt";
}

Am I just completely missing the boat here? Thank you again!
 
T

Tom Porterfield

Dogmar said:
Thank you very much.

This is a bit confusing. Here is what it does:

m_StartDate = 20060309
m_EndDate = 20060313

Only one file named 20060312.txt gets created...

The code as it stands is:

TimeSpan Diff = m_EndTime.Subtract(m_StartTime);
int j = Diff.Days;
for(int i =0;i<j;i++)
{
FileName = FileDirectory + "\\" +
this.m_StartTime.AddDays(i).ToString("yyyyMMdd")+ "_" +
FileIndx.ToString() + ".txt";
}

Am I just completely missing the boat here? Thank you again!

Where is the code that actually creates the file? It needs to be inside
your loop.
 
S

SP

Dogmar Hoffman said:
Hello,

I am pretty new to C# and have been surfing this forum. It has been a
great help. I am hoping that you can help with this issue:

I have the following:

TimeSpan Diff = new TimeSpan();
Diff = m_EndTime.Subtract(m_StartTime);
//make array to store all the dates to print
for(int j = Diff.Days;j>0;j--)
{
//change to read array in a loop and make each file
FileName = FileDirectory + "\\" + this.m_StartTime.ToString("yyyyMMdd")
+ j + FileIndx.ToString() + ".txt";
}

Basically, I would need to incriment the m_StartTime j times. Then I
need to store them in an array and run through the dates there to create
a filename for each one. Unless there is a better way. The end result
should just be like this:

You can loop through dates like:

for(DateTime dt = startDate; dt <= endDate; dt = dt.AddDays(1))
{
}

SP
 
Top