-2

I have a Int64 variable (eg : 637383629352337674). I have to use this variable to create a DateTime object which uses this number. So I used the following constructor to create the DateTime object :

public DateTime (long ticks);

But I observed that I am not able to get the milliseconds printed when I use the ToString() method of DateTime class.

It would be possible to get the millisecinds printed with DateTimeOffset being concatenated with the ToString() of DateTime object, but I don't want to do that.

How can I get a DateTime object with milliseconds ?

  • 1
    your code `new DateTime(637383629352337674).Millisecond` returns 233... so you are ... misleading in your post. Please [edit] to clarify what you actually need. – Alexei Levenkov Apr 20 '21 at 06:20
  • https://learn.microsoft.com/en-us/dotnet/standard/base-types/how-to-display-milliseconds-in-date-and-time-values – Velja Radenkovic Apr 20 '21 at 06:20

2 Answers2

3

Your datetime has milliseconds, just make sure you include them in the ToString format specifier (the default format string doesn't include milliseconds):

.ToString("HH:mm:ss.ffffff")

see example

For more info on date time format strings, see the documentation

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

If you are asking about including the milliseconds when converting the DateTime to a String, google DateTime format string. First hit is https://www.c-sharpcorner.com/blogs/date-and-time-format-in-c-sharp-programming1

You can include the milliseconds like this: myDate.ToString("HH:mm:ss.fff");

You can use up to seven "f"s there.

lot_styx
  • 25
  • 4