473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Incrimenting Dates in C#

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

*** Sent via Developersdex http://www.developersdex.com ***
Mar 22 '06 #1
7 5776
Dogmar Hoffman schrieb:
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

*** Sent via Developersdex http://www.developersdex.com ***


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

Martin
Mar 22 '06 #2
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!

*** Sent via Developersdex http://www.developersdex.com ***
Mar 22 '06 #3
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
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 22 '06 #4
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!

*** Sent via Developersdex http://www.developersdex.com ***
Mar 22 '06 #5
Dogmar Hoffman wrote:
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.
--
Tom Porterfield

Mar 22 '06 #6
Ahh yes. That was it. Now it is working. Thank you all very much!
*** Sent via Developersdex http://www.developersdex.com ***
Mar 22 '06 #7
SP

"Dogmar Hoffman" <it******@itbranch.com> wrote in message
news:u3**************@TK2MSFTNGP10.phx.gbl...
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

Mar 22 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Riley | last post by:
The date fields being saved by a VB program were being saved as #2003-11-22#. For reasons unknown to me these dates began to be saved as "11/22/2003" All of these dates were made dates with the...
5
by: PW | last post by:
<rant> Sorry guys, but I just have to whinge. Dates in ASP are a total pain in the butt! I seem to get caught out so many times. I realise its my own fault, but going from the posts in this...
10
by: Colin Steadman | last post by:
I'm a stupid ASP programmer and I dont do Javascript (except for very simple tasks anyway), and I'm in a bit of a predicament. I've used a javascript table sorting script from here: ...
1
by: Don Sealer | last post by:
I have a report that includes 5 different subreports. I'd like to be able to open this report using a date function (Start Date and End Date). I'd like all five subreports to show the data from...
2
by: Rachel Suddeth | last post by:
Is there a way to have the non-selectable dates (those before MinDate and after MaxDate) draw differently so my users can see right away what dates aren't allowed? I'm not seeing it... ...
12
by: Dixie | last post by:
I am trying to calculate the number of workdays between two dates with regards to holidays as well. I have used Arvin Meyer's code on the Access Web, but as I am in Australia and my date format is...
1
by: pitfour.ferguson | last post by:
My dbase has the start date and end date of each visit. How can I ask Access to list the day of the week of the start (easy), end (easy) and, more importantly, the dates of the visit itself - ie...
7
by: evilcowstare via AccessMonster.com | last post by:
Hi, I have searched the forum for answers on this and to be honest as a novice I find it a bit confusing so apologies if it is simple. There are some searches that I want to apply to my database....
2
by: Jim Carlock | last post by:
(1) Does PHP provide any way to handle dates prior to 1980? I know there's problems with Microsoft Windows NT and all Windows NT operating systems will allow a date prior to 1980 to be placed...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.