Get user input, convert the data and then output it into a text box
I'm trying to compile my first C# application (based on Visual Studio) ... also using Windows Forms for input (from user) and output.
User puts numbers into six text boxes (e.g. 2009 20 02 02:49:35) and then when the 'Convert' button is clicked, the program outputs E1234FB3278DC0 in a different text box.
Not sure if this is relevant but E1234FB3278DC0 = 63370694975000000 (in decimal).
oh also, I'm not sure about convertedText.writeline... should it be this.textBox7 = microseconds; ?
String dateString = yyyy.Text + dd.Text + mm.Text + hh.Text + mm.Text + ss.Text;
开发者_如何学C DateTime timestamp = DateTime.ParseExact(dateString, "yyyy dd mm hh:mm:ss", CultureInfo.CurrentCulture);
long ticks = timestamp.Ticks;
long microseconds = ticks / 10;
convertedText.WriteLine(microseconds.ToString("X"));
Thanks in advance.. And I gotta thank Luxspes for the orginal version.
Some tips about this code snippet.
String dateString = yyyy.Text + dd.Text + mm.Text + hh.Text + mm.Text + ss.Text;
DateTime timestamp = DateTime.ParseExact(dateString, "yyyy dd mm hh:mm:ss", CultureInfo.CurrentCulture);
First of all, it's really strange that you use the same "mm"-object for months and minutes. The same problem with format specifier. To parse month you should use 'M'.
long ticks = timestamp.Ticks;
long microseconds = ticks / 10;
convertedText.WriteLine(microseconds.ToString("X"));
So, if your date parsed successfully you'll get the number of microseconds that have elapsed since 12:00:00 midnight, January 1, 0001. It's E1234FB3278DC0 in hexadecimal (for the date in your question). But in your case date represented in the seconds. So, the number of microseconds will be always.
timestamp.Millisecond*1000;
I have no idea about the type of convertedText object. But it seems to me that's not a problem.
Try to use the following code:
String dateString = yyyy.Text+dd.Text+M.Text+hh.Text+mm.Text+ss.Text;
DateTime dateTime = DateTime.ParseExact(dateString, "yyyy dd M hh:mm:ss", CultureInfo.CurrentCulture);
long microseconds = dateTime.Ticks/10;
convertedText.Text = microseconds.ToString("X");
精彩评论