Solved

Increment DateTime Day, Hour, Minute, Second, Millisecond?

Posted on 2012-12-21
8
466 Views
Last Modified: 2012-12-22
I need to increment two datetime's by Day, Hour, Minute, Second, Millisecond.

 Help!

 private void btnLoadListbox_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();

            var examDate = new DateTime(2012, 1, 1);
            var expireDate = new DateTime(2015, 1, 1);

            for (int i = 0; i < 100; i++)
            {
                var examDatePlus1Day = examDate.AddDays(1);
                var expireDatePlus1Day = expireDate.AddDays(1);

                list.Add(examDatePlus1Day + "," + expireDatePlus1Day);
            }

            foreach (var s in list)
            {
                listBox1.Items.Add(s);
            }
        }
0
Comment
Question by:pointeman
8 Comments
 
LVL 15

Expert Comment

by:Ess Kay
ID: 38714504
Assuming you want to add 30 minutes to a given DateTime, you can use AddMinutes.

TestTime.AddMinutes(30);


Another way of doing it:

DateTime TestTime = DateTime.Parse("22 Jun 2009 14:20:00");
// Add 30 minutes
TestTime = TestTime + TimeSpan.Parse("00:30:00");
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 38714506
if you are to run a loop,     yourtimevariable.addseconds(1);
should do the trick
0
 

Author Comment

by:pointeman
ID: 38714611
I need to increment two datetime's by Day, Hour, Minute, Second, Millisecond.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 167 total points
ID: 38714642
You can chain your arithmetic together:

DateTime result = examDate.AddDays(1).AddHours(1).AddMinutes(1).AddSeconds(1).AddMilliseconds(1);

Open in new window

0
 

Author Comment

by:pointeman
ID: 38714771
I did the chaining and it didn't work. Now looking at my code example I notice the problem is that I'm incrementing the original date repeatedly. I need to Add to the newly incremented date.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 167 total points
ID: 38714840
Change this:

            for (int i = 0; i < 100; i++)
            {
                var examDatePlus1Day = examDate.AddDays(1);
                var expireDatePlus1Day = expireDate.AddDays(1);

                list.Add(examDatePlus1Day + "," + expireDatePlus1Day);
            }

To this:

            for (int i = 0; i < 100; i++)
            {
                examDate = examDate.AddDays(1);
                expireDate = expireDate.AddDays(1);

                list.Add(examDate + "," + expireDate);
            }
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 166 total points
ID: 38715039
I think using TimeSpan produces a cleaner code:

         DateTime dt = DateTime.Now;
         TimeSpan ts = new TimeSpan(1,0,0,0);  // 1 day, 0 hours, 0 minutes, 0 seconds
     
         for (var i=0; i<100; i++)
         {
             Console.WriteLine(dt);
             dt += ts;  // The date is increased here
         }
0
 

Author Closing Comment

by:pointeman
ID: 38715622
Excellent, thank you all.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question