Solved

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

Posted on 2012-12-21
8
465 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 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

15 Experts available now in Live!

Get 1:1 Help Now