How do I convert a datetime with milliseconds to a string in C#?
I want to convert:
5/25/2010 12:54:56:000
to:
052520101254开发者_StackOverflow中文版56000
How do I do that in C#?
You can use a custom format string. Example:
string formatted = DateTime.Now.ToString("MMddyyyyHHmmssfff");
Try this:
DateTime.Now.ToString("HH:mm:ss.ffffff");
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Using the ToString() method on your DateTime, passing a custom format string: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx (I assume you currently have a DateTime object.)
To get the total Milliseconds only as a string use this:
TimeSpan value = (DateTime.Now - DateTime.MinValue);
string milliseconds = value.TotalMilliseconds.ToString();
If you want to store and/or compare the DateTime value, then I suggest you use the .Ticks property of the DateTime as a string, because you can reconstruct a DateTime value passing the ticks as a constructor argument.
精彩评论