tags:

views:

48

answers:

1

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;
        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.

+1  A: 

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");
MAKKAM
the type of convertedText is text box. but it's wrong somehow (got an error - System.Windows.Forms.TextBox' does not contain a definition for 'WriteLine')
Mark
hang in there, I just need to make sure it's working first. But I got an error when I click the 'Convert' button.here's the new code: private void button1_Click(object sender, EventArgs e) { String dateString = yyyy.Text + mm.Text + dd.Text + hh.Text + M.Text + ss.Text; DateTime timestamp = DateTime.ParseExact(dateString, "yyyy dd MM hh:mm:ss", CultureInfo.CurrentCulture); long ticks = timestamp.Ticks; long microseconds = ticks / 10; convertedText.Text = microseconds.ToString("X"); }
Mark
I see. Your code doesn't compile. Try to change: 1) date format: "yyyy dd M hh:mm:ss"; 2) add TextBox for minutes 3) use convertedText.Text property to assign text.
MAKKAM
Ok, I've changed the format and a text box for minutes already exists. And I'm not sure what you mean by 'use convertedText.Text property to assign text'. I'm using: convertedText.Text = microseconds.ToString("X"); } Is this correct? It compiles fine, but when I click the 'Convert' button, I get this message: String was not recognized as a valid DateTime
Mark
Wait... what format do I change it to ? can you please post the code..? I'm completely new to c#..
Mark
can anybody help me? :(
Mark