Correct format for TimeSpan

This is a discussion on Correct format for TimeSpan within the C# Programming forums, part of the General Programming Boards category; Hi. I need help. I am working on making a simple alarm clock and I want to get all the ...

  1. #1
    Registered User Grayson_Peddie's Avatar
    Join Date
    May 2002
    Posts
    96

    Correct format for TimeSpan

    Hi. I need help. I am working on making a simple alarm clock and I want to get all the information from the two NumericUpDown controls and one button. I had limit the hours between 1-12 and limited minutes to 0-59, and set my button to change the text from AM-PM or PM to AM. Here's my code:

    Code:
            public TimeSpan tsTime;
            
            private void nHour_ValueChanged(object sender, System.EventArgs e)
            {
                if(nHour.Value > 12)
                {
                    nHour.Value = 1;
                }
                if(nHour.Value == 0)
                {
                    nHour.Value = 12;
                }
                tsTime = TimeSpan.Parse(nHour.ToString()+":"+nMinute.ToString()+":00");
            }
    
            private void btnPMAM_Click(object sender, System.EventArgs e)
            {
                if(btnPMAM.Text == "AM") 
                {
                    btnPMAM.Text = "PM";
                }
                else
                {
                    btnPMAM.Text = "AM";
                }
                tsTime = TimeSpan.Parse(nHour.ToString()+":"+nMinute.ToString()+":00");
            }
    
            private void nMinute_ValueChanged(object sender, System.EventArgs e)
            {
                if(nMinute.Value  > 59)
                {
                    nMinute.Value =  0;
                }
                if(nMinute.Value  <  0)
                {
                    nMinute.Value = 59;
                }
                tsTime = TimeSpan.Parse(nHour.ToString()+":"+nMinute.ToString()+":00");
            }
    What is the correct format for passing a time to tsTime TimeSpan object? I just have ":00" for the second since the user can only set the hours and the minutes. And, of course, since I do have a clock control that I made by creating my user control, the tsTime will be passed over to a string variable, so that, when the string variable has the same string as the clock's, the alarm label will show up.
    View in Braille.
    http://www.future-gpnet.com/braille.jpg

    Like a bolt out of the BLUE,
    Fate steps up and sees you THROUGH,
    When you wish upon a STAR
    YOUR DREAMS COME TRUE.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I am working on making a simple alarm clock and I want to get all the information from the two NumericUpDown controls and one button. I had limit the hours between 1-12 and limited minutes to 0-59, and set my button to change the text from AM-PM or PM to AM.
    It would be a lot easier and a lot more readable to switch to the 24h style to have 0:00 - 23:59.

    From this, you cannot construct a timespan. A timespan is the difference of two times. Like the timespan between now and lunch is 1 hour 30 minutes. Two times and one absolute value that marks the difference. Construct a datetime object from your values. Subtract the current time ( static function of datetime ). The timespan you get is the time that has to pass until the set time is reached.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User Grayson_Peddie's Avatar
    Join Date
    May 2002
    Posts
    96
    If I use a DateTime method instead of TimeSpan, I think that a DateTime.Parse() requires me to add a date, too. But you can correct me if I'm wrong.

    Can you show me the code on how to parse the Hours and Minutes without a Date?
    View in Braille.
    http://www.future-gpnet.com/braille.jpg

    Like a bolt out of the BLUE,
    Fate steps up and sees you THROUGH,
    When you wish upon a STAR
    YOUR DREAMS COME TRUE.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Well, you do need a date, but why not use it ? You have a day and time and a day and time of the alarm. Use those
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User Grayson_Peddie's Avatar
    Join Date
    May 2002
    Posts
    96
    Okay thanks but date is unnessessary to me since this is going to be a simple alarm clock. My program is going to be displaying a time but no date.
    View in Braille.
    http://www.future-gpnet.com/braille.jpg

    Like a bolt out of the BLUE,
    Fate steps up and sees you THROUGH,
    When you wish upon a STAR
    YOUR DREAMS COME TRUE.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming with the bitmap file format
    By redneon in forum C++ Programming
    Replies: 6
    Last Post: 12-01-2007, 05:12 PM
  2. what format
    By Stream in forum C Programming
    Replies: 1
    Last Post: 09-07-2005, 04:55 AM
  3. about to format :/
    By MisterSako in forum Tech Board
    Replies: 7
    Last Post: 06-13-2005, 07:51 PM
  4. Can't Format C
    By caroundw5h in forum Tech Board
    Replies: 40
    Last Post: 04-26-2004, 09:57 AM
  5. wsprintf and format specifiers
    By incognito in forum Windows Programming
    Replies: 2
    Last Post: 01-03-2004, 09:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21