简体   繁体   中英

C# program crash

I'm trying to get my first ever C# application working as intended. :)

This application is a time converter, which allows user to input numbers into six different text boxes. For example, user puts 2009 into yyyy.Text, 20 into dd.Text, 02 into M.text, 02 into hh.Text, 49 into mm.Text and 35 into ss.Text. Then the program converts the numbers into a hexadecimal string.

For example, 2009 20 02 02:49:35 -> 63370694975000000 -> E1234FB3278DC0

private void button1_Click(object sender, EventArgs e)
        {
            String dateString = yyyy.Text + dd.Text + M.Text + hh.Text + mm.Text + ss.Text;
            DateTime timestamp = DateTime.ParseExact(dateString, "yyyy dd M  hh:mm:ss", CultureInfo.CurrentCulture);
            long ticks = timestamp.Ticks;
            long microseconds = ticks / 10;
            convertedText.Text = microseconds.ToString("X");
        }

The application is compiled fine, but after putting numbers into the text boxes and clicking the 'Convert' button, the program crashes. The error: Additional information: String was not recognized as a valid DateTime.

Am I using the wrong DataTime format? or something? :(

Thanks in advance... I wanna get this working :)

我认为您需要在格式中声明的空格和冒号。

When making the dateString , don't forget to insert spaces. Use

String.Format("{0} {1} {2} {3}:{4}:{5}",yyyy.Text, dd.Text, M.Text, hh.Text, mm.Text, ss.Text)

And, place a try-catch block - it's the easiest way to catch exceptions when converting, (although not recommended for a good program), in case the user inputs some insane numbers and text.

Your dateString does not correspond to the format you specified - you have a string without any separators (like spaces or colons). Format the string accordingly to your format. Also, it is always a good idea to use format utils. But I think you should use string, not String:

string dateString = string.Format("{0} {1} {2} {3}:{4}:{5}",
    yyyy.Text, dd.Text, M.Text, hh.Text, mm.Text, ss.Text);

Try to insert a breakpoint on the DateTime timestamp = DateTime.ParseExact(dateString, "yyyy dd M hh:mm:ss", CultureInfo.CurrentCulture) line.

Then before you execute that command, hover your mouse over dateString . Then I think you will see that dateString does nto match the format you have provided to ParseExact at all.

When the string matches the format you have given, it should work fine.

Good luck! :)

Try this

Solution 1:

private void button1_Click(object sender, EventArgs e) 
{ 
   DateTime timestamp = new DateTime( 
                                Convert.ToInt32(yyyy.Text) 
                                , Convert.ToInt32(M.Text) 
                                , Convert.ToInt32(dd.Text) 
                                , Convert.ToInt32(hh.Text) 
                                , Convert.ToInt32(mm.Text) 
                                , Convert.ToInt32(ss.Text)); 

    long ticks = timestamp.Ticks; 
    long microseconds = ticks / 10; 
    convertedText.Text = microseconds.ToString("X"); 
} 

Solution 2:

private void button1_Click(object sender, EventArgs e) 
{ 

string dateString = string.Format("{0}/{1}/{2} {3}:{4}:{5}", M.Text,dd.Text,yyyy.Text, hh.Text, mm.Text, ss.Text); 
long ticks = Convert.ToDateTime(dateString).Ticks;
long microseconds = ticks / 10;
convertedText.Text = microseconds.ToString("X");        
} 

Output:

E1234FB3278DC0 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM