?
Solved

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

Posted on 2012-12-21
8
Medium Priority
?
473 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 668 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 86

Accepted Solution

by:
Mike Tomlinson earned 668 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 664 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 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