Solved

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

Posted on 2012-12-21
8
464 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
 
LVL 74

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now