0

I am trying make a clock. The hour is a string. I want to put that hour into a char array so i can separate the hour into one or two indexes. That way i can use a case on the individual indexes to ultimately bind it to a grid and draw a line for the digital time..

So, the hour is converted to an array. But i want to take the first index 0 and store it into a string or int so i can pass it into a function where i can use a case on it. if i leave it as a char and convert it to an int i get a number like 50 which is no good.

So, when i try to assign the first index of the array to a string it wont let me convert from array to string.

 hr1 = hours[0];

What is my best option of seperating the hour into separate indexes and then converting it over to the proper int? Also, the time is on 24 hour and i would like it to be 12 hour.

private void _timer_Elapsed(object sender, EventArgs e)
{            
    DateTime now = DateTime.Now;
    //DigitalTime = now.ToString("hh:mm:ss tt");
    //DigitalTime = now.ToString();
    //DigitalTime = DateTime.Now.Hour.ToString();

    SecondAngle = now.Second * 6;
    MinuteAngle = now.Minute * 6;
    HourAngle = (now.Hour * 30) + (now.Minute * 0.5);

    string hrs, hr1, hr2;           

    char[] hours = new char[15];

    hrs = DateTime.Now.Hour.ToString("hh:mm:ss tt");
    hours = hrs.ToCharArray();      

    if (hours.Length > 1)
    {                    
        hr1 = hours[0];        // error - 
        hr2 = hours[1];                  
       // SetHourDigit1(Convert.ToInt32(hr1));
    }
    else
    {
       // hr1 = '0';
        hr2 = hours[0];                    
    } 
}         

public void SetHourDigit1(int num)
{
    switch (num)
    {
        case 0:
            MessageBox.Show("num" + num);
            break;
        case 1:
            MessageBox.Show("num" + num);
            break;
        case 2:
            break;
    }
}

3 Answers 3

1

I would avoid messing with strings and char arrays altogether. Use arithmetic instead:

int hour = DateTime.Now.Hour;
int leastSignificantDigit = hour % 10;
int mostSignificantDigit = hour / 10;
// Use one of these as input for your switch statement.

% is the modulo operator; the remainder of a division by 10 in this case.

Edit: I noticed you want to have a 12-hour clock. You can add some additional computation for this. Replacement for the first line of code:

int hour = DateTime.Now.Hour % 12;
if (hour == 0) hour = 12;
Sign up to request clarification or add additional context in comments.

Comments

1
if (hours.Length > 1)
            {

                hr1 = hours[0].ToString();        // no error - 
                hr2 = hours[1].ToString();

               // SetHourDigit1(Convert.ToInt32(hr1));

            }

but if you want to get parts of time use this:

dateparts = datestring.splite(':');

string hour = dateparts[0];
string minute = dateparts[1];

string s = dateparts[2];

now you have hour,minute,second and t. because of you can trust the parts use int.parse to convert them to int.

int nhour = int.parse(hour);
int nminute = int.parse(minute);
int nsecond = int.parse(s);

for 24 hours

hrs = DateTime.Now.Hour.ToString("HH:mm:ss");

This is a usefull link for u: DateTime.ToString() Pattern

3 Comments

Is there away to change it to 12 hour time without adding this line? "hh:mm:ss tt"
h for 12 hours and H for 24 hours
You can use ToCharArray() to get Hour parts easily
0

Use the modulo (%) operator to convert the 24 hour value to 12 hours, and also to get the second digit of the two digit number. There is no reason to format it as a string and then convert it back to numbers.

private void _timer_Elapsed(object sender, EventArgs e) { 

  DateTime now = DateTime.Now;

  int hour12 = now.Hour % 12;

  SecondAngle = now.Second * 6;
  MinuteAngle = now.Minute * 6;
  HourAngle = (hour12 * 30) + (now.Minute * 0.5);

  SetHourDigit1(hour12 / 10);
  SetHourDigit2(hour12 % 10);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.