Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

.net - DateTime.ParseExact omitting milliseconds in c#?

I am trying to convert a string to Datetime in c#. String datestring= 2013/03/18 10:54:07.679

  1. I tried DateTime dt=DateTime.ParseExact(datestring, "yyyy/MM/dd HH:mm:ss.fff",null); The result is {3/18/2013 10:54:07 AM}

  2. I tried DateTime.TryParseExact(datestring,"yyyy/MM/dd HH:mm:ss.fff",CultureInfo.InvariantCulture,DateTimeStyles.None,out dttt); The result is {3/18/2013 10:54:07 AM}

In both the above cases its ommitting millisecons(679).

How can I convert it to datetime correctly by keeping the milliseconds?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

it's not ommitting you are just checking through debugger and debugger shows it using AM or PM ,it doenot show milliseconds part.

Try This:

DateTime dt=DateTime.ParseExact(datestring, "yyyy/MM/dd HH:mm:ss.fff",null);
Console.WriteLine(dt.ToString("yyyy/MM/dd HH:mm:ss.fff"));

EDIT: from your comment But I need the answer in Datetime instead of string

You have already the DateTime including MilliSeconds just debugger is not showing because (As mentioned in comment by Ant P) Debugger calls the Parameterless overload of ToString() method which shows the DateTime without MilliSeconds.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...