CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2010
    Location
    England
    Posts
    21

    [RESOLVED] Best way to validate time from user?

    Hello, everyone. I have two text boxes (timeTakenA and timeTakenB), and I want the user to be able to enter a time into these text boxes (hh:mm:ss). Then when a button is pressed, the two times are added together and a total time taken is displayed.

    I must admit, I'm really struggling to find a good way of doing this. Can anyone give me some ideas?

    Thanks in advance.

    **I'm using Visual Studio 2010 .NET 4

  2. #2
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: Best way to validate time from user?

    how about the Timespan struct ... have you considered that ? might work for ya.



    Code:
                string[] time1, time2;
                string result;
    
    
                time1 = textbox1DotText.Split(':');
                time2 = textbox2DotText.Split(':');
                result = (new TimeSpan(Int32.Parse(time1[0]), Int32.Parse(time1[1]), Int32.Parse(time1[2])) +
                         new TimeSpan(Int32.Parse(time2[0]), Int32.Parse(time2[1]), Int32.Parse(time2[2]))).ToString();
    
                // Trust in The Force, Luke ....
    Last edited by ThermoSight; March 20th, 2011 at 04:16 PM.

  3. #3
    Join Date
    Nov 2009
    Posts
    34

    Re: Best way to validate time from user?

    I did this quickly, it could probably be a little cleaner, but should work:

    Code:
    namespace add_date_time
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }
    
        private void btnAddTimes_Click(object sender, EventArgs e)
        {
          int AHours = int.Parse(txtBoxTimeA.Text.Split(':')[0]);
          int AMinutes = int.Parse(txtBoxTimeA.Text.Split(':')[1]);
          int ASeconds = int.Parse(txtBoxTimeA.Text.Split(':')[2]);
    
          int BHours = int.Parse(txtBoxTimeB.Text.Split(':')[0]);
          int BMinutes = int.Parse(txtBoxTimeB.Text.Split(':')[1]);
          int BSeconds = int.Parse(txtBoxTimeB.Text.Split(':')[2]);
    
          int TotalHours = AHours+BHours;
          int TotalMinutes = AMinutes+BMinutes;
          int TotalSeconds = ASeconds+BSeconds;
    
          while (TotalSeconds > 60 | TotalSeconds == 60)
          {
            TotalSeconds = (TotalSeconds - 60);
            TotalMinutes = (TotalMinutes + 1);
          }
    
          while (TotalMinutes > 60 | TotalMinutes == 60)
          {
            TotalMinutes = (TotalMinutes - 60);
            TotalHours = (TotalHours + 1);
          }
    
          int TotalDays = 0;
          while (TotalHours > 24 | TotalHours == 24)
          {
            TotalHours = (TotalHours - 24);
            TotalDays = (TotalDays + 1);
          }
    
          txtBoxTotalTimeTaken.Text = TotalDays + "d :" + TotalHours + "h :" + TotalMinutes + "m :" + TotalSeconds + "s";
    
        }
      }
    }
    Last edited by saldous; March 20th, 2011 at 05:03 PM.

  4. #4
    Join Date
    Nov 2010
    Location
    England
    Posts
    21

    Thumbs up Re: Best way to validate time from user?

    I can't thank you guys enough. It's much appreciated. Your example worked perfectly Saldous. Good Job.

  5. #5
    Join Date
    Nov 2009
    Posts
    34

    Re: [RESOLVED] Best way to validate time from user?

    ThermoSight's code is a lot nicer. I learnt something too.

  6. #6
    Join Date
    Nov 2010
    Location
    England
    Posts
    21

    Thumbs up Re: [RESOLVED] Best way to validate time from user?

    Ahh...I didn't see that edit. Thanks ThermoSight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured