Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a datetime with milliseconds to a string in C#?

Tags:

c#

datetime

I want to convert:

5/25/2010 12:54:56:000 

to:

05252010125456000 

How do I do that in C#?

like image 375
acadia Avatar asked May 25 '10 16:05

acadia


People also ask

How do you convert DateTime to milliseconds?

How do you convert datetime to milliseconds? A simple solution is to get the timedelta object by finding the difference of the given datetime with Epoch time, i.e., midnight 1 January 1970. To obtain time in milliseconds, you can use the timedelta. total_seconds() * 1000 .

How are milliseconds represented in date format?

To display the millisecond component of a DateTime value If you're working with the string representation of a date, convert it to a DateTime or a DateTimeOffset value by using the static DateTime. Parse(String) or DateTimeOffset. Parse(String) method.

How do you write milliseconds in time?

Millis is the popular abbreviation for milliseconds. The formal one would be ms.

Can we convert DateTime to string in C#?

The ToString() method of the DateTime class is used to convert a DateTime date object to string format. The method takes a date format string that specifies the required string representation.


2 Answers

You can use a custom format string. Example:

string formatted = DateTime.Now.ToString("MMddyyyyHHmmssfff");
like image 177
Guffa Avatar answered Oct 20 '22 14:10

Guffa


Try this:

DateTime.Now.ToString("HH:mm:ss.ffffff");

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

like image 27
anishMarokey Avatar answered Oct 20 '22 14:10

anishMarokey