Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-12-21
8
Medium Priority
?
478 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 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