Converting the date time to display milliseconds in C#
Asked Answered
H

3

6

I want to show the milliseconds, but ToString shows the milliseconds as 00000.

I am providing the code below, with the output at each step.

  1. String currentDateTime = DateTime.Now.ToString("G");

    Output - 7/27/2011 3:05:31 PM

  2. System.DateTime dateTime = System.DateTime.Parse(currentDateTime);

    Output - 7/27/2011 3:05:31 PM

  3. String dateTimeStr = dateTime.ToString("hh.mm.ss.ffffff", "en-US");

Output - 03.05.32.000000

I want to show the output with the milliseconds , eg 03.05.32.33456

If I used ParseExact instead of Parse, I am getting an exception. I know that I can use TryParseExact, but that solution might not be suitable , as I need a generic solution to this problem .

Can someone help me in this.

Thanks in advance. Sujay

Helpmeet answered 27/7, 2011 at 9:46 Comment(3)
I guess that the third option is correct. Probably the content of the DateTime structure has the millisecs equals to zero.Sukin
No the milliseconds are not zero. Please look at the accepted answer.Helpmeet
That's exactly what I mean. The third assignment is correct, while the first two have no sense. Why are you using all of them?Sukin
P
6

By using the same dateTime object that you previously built from a string ("7/27/2011 3:05:31 PM" without any milliseconds), you're losing the milliseconds.

If you were to convert Now to a string directly, you would not lose the milliseconds:

String dateTimeStr = DateTime.Now.ToString("hh.mm.ss.ffffff", "en-US");
Phyte answered 27/7, 2011 at 9:57 Comment(0)
L
8

Not sure why you are moving from DateTime -> string -> DateTime This should display milliseconds

DateTime dt = DateTime.Now;
dt.ToString("hh:mm:ss.fff") 

Please edit the post if you are not looking on these lines for additional info

Lally answered 27/7, 2011 at 9:51 Comment(1)
Your code wont display millisecond. Please read my post carefully.Helpmeet
P
6

By using the same dateTime object that you previously built from a string ("7/27/2011 3:05:31 PM" without any milliseconds), you're losing the milliseconds.

If you were to convert Now to a string directly, you would not lose the milliseconds:

String dateTimeStr = DateTime.Now.ToString("hh.mm.ss.ffffff", "en-US");
Phyte answered 27/7, 2011 at 9:57 Comment(0)
T
3

In your code you first serialize the DateTime to string using the standard "G" format, which doesn't have miliseconds. So sure, you get 000000 later when you parse this string back:

        String currentDateTime = DateTime.Now.ToString("G");
        Console.WriteLine(currentDateTime);

        System.DateTime dateTime = System.DateTime.Parse(currentDateTime);

        Console.WriteLine(dateTime.ToString("hh.mm.ss.ffffff"));
Telefilm answered 27/7, 2011 at 9:53 Comment(2)
Peter - Please see the output of 3. I had also used dateTime.ToString("hh.mm.ss.ffffff", "en-US"); , but I get a wrong output. The millisecond are 0000.Helpmeet
Are you talking about the serialization , yes I did . I want the output without serialization.Helpmeet

© 2022 - 2024 — McMap. All rights reserved.