Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between fff and ms in datetime?

Tags:

c#

datetime

What is the difference between fff and ms in datetime?

DateTime dt = DateTime.Now;
string s1 = dt.ToString("yyyy-MM-dd HH:mm:ss.fff");
string s2 = dt.ToString("yyyy-MM-dd-hh-mm-ss-ms");

The output will be as below:

2018-12-03 14:28:23.357

2018-12-03-02-28-23-2823

You see that 357 is different from 2823. What is the reason? Thanks.

Edit: Thanks all of you. I wonder how ms came out of my mind. Maybe I mix it with Oracle. Haha.

like image 354
Robin Sun Avatar asked Dec 24 '22 02:12

Robin Sun


2 Answers

There isn't any ms format-key to formatting DateTime in C#. The m stands for minute and the s stands for second. So, ms is just a combination of minute and second along together. For formatting milliseconds in DateTime the pattern uses f and F. What you did actually, was putting a m beside a s. So you got 28 beside 23 which results 2823.

For more information, see the doc: How to: Display Milliseconds in Date and Time Values

like image 199
amiry jd Avatar answered Dec 25 '22 14:12

amiry jd


Take a look at the docs: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings?view=netframework-4.7.2

There is no ms specifier; what you have is the concatenation of the m (minutes, 28 in this case) and the s specifier (seconds, 23), hence 2823.

like image 29
Flydog57 Avatar answered Dec 25 '22 16:12

Flydog57