How can I exactly construct a time stamp of actual time with milliseconds precision?
I suspect you mean millisecond accuracy. DateTime has a lot of precision, but is fairly coarse in terms of accuracy. Generally speaking, you can't. Usually the system clock (which is where DateTime.Now gets its data from) has a resolution of around 10-15 ms. See Eric Lippert's blog post about precision and accuracy for more details.
If you need more accurate timing than this, you may want to look into using an NTP client.
However, it's not clear that you really need millisecond accuracy here. If you don't care about the exact timing - you just want to show the samples in the right order, with "pretty good" accuracy, then the system clock should be fine. I'd advise you to use DateTime.UtcNow rather than DateTime.Now though, to avoid time zone issues around daylight saving transitions, etc.
If your question is actually just around converting a DateTime to a string with millisecond precision, I'd suggest using:
string timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff",
CultureInfo.InvariantCulture);
(Note that unlike your sample, this is sortable and less likely to cause confusion around whether it's meant to be "month/day/year" or "day/month/year".)
Answer from Jon Skeet on Stack OverflowHow can I exactly construct a time stamp of actual time with milliseconds precision?
I suspect you mean millisecond accuracy. DateTime has a lot of precision, but is fairly coarse in terms of accuracy. Generally speaking, you can't. Usually the system clock (which is where DateTime.Now gets its data from) has a resolution of around 10-15 ms. See Eric Lippert's blog post about precision and accuracy for more details.
If you need more accurate timing than this, you may want to look into using an NTP client.
However, it's not clear that you really need millisecond accuracy here. If you don't care about the exact timing - you just want to show the samples in the right order, with "pretty good" accuracy, then the system clock should be fine. I'd advise you to use DateTime.UtcNow rather than DateTime.Now though, to avoid time zone issues around daylight saving transitions, etc.
If your question is actually just around converting a DateTime to a string with millisecond precision, I'd suggest using:
string timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff",
CultureInfo.InvariantCulture);
(Note that unlike your sample, this is sortable and less likely to cause confusion around whether it's meant to be "month/day/year" or "day/month/year".)
This should work:
DateTime.Now.ToString("hh.mm.ss.ffffff");
If you don't need it to be displayed and just need to know the time difference, well don't convert it to a String. Just leave it as, DateTime.Now();
And use TimeSpan to know the difference between time intervals:
Example
DateTime start;
TimeSpan time;
start = DateTime.Now;
//Do something here
time = DateTime.Now - start;
label1.Text = String.Format("{0}.{1}", time.Seconds, time.Milliseconds.ToString().PadLeft(3, '0'));
DateTime.Now.ToString("yyyyMMddHHmmss"); // case sensitive
C# Examples by Jan Slama has great examples on this, check it out:
// create date time 2008-03-09 16:05:07.123 DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123); String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24 String.Format("{0:m mm}", dt); // "5 05" minute String.Format("{0:s ss}", dt); // "7 07" second String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M. String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone
// month/day numbers without/with leading zeroes String.Format("{0:M/d/yyyy}", dt); // "3/9/2008" String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008" // day/month names String.Format("{0:ddd, MMM d, yyyy}", dt); // "Sun, Mar 9, 2008" String.Format("{0:dddd, MMMM d, yyyy}", dt); // "Sunday, March 9, 2008" // two/four digit year String.Format("{0:MM/dd/yy}", dt); // "03/09/08" String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008"
Standard DateTime Formatting
String.Format("{0:t}", dt); // "4:05 PM" ShortTime String.Format("{0:d}", dt); // "3/9/2008" ShortDate String.Format("{0:T}", dt); // "4:05:07 PM" LongTime String.Format("{0:D}", dt); // "Sunday, March 09, 2008" LongDate String.Format("{0:f}", dt); // "Sunday, March 09, 2008 4:05 PM" LongDate+ShortTime String.Format("{0:F}", dt); // "Sunday, March 09, 2008 4:05:07 PM" FullDateTime String.Format("{0:g}", dt); // "3/9/2008 4:05 PM" ShortDate+ShortTime String.Format("{0:G}", dt); // "3/9/2008 4:05:07 PM" ShortDate+LongTime String.Format("{0:m}", dt); // "March 09" MonthDay String.Format("{0:y}", dt); // "March, 2008" YearMonth String.Format("{0:r}", dt); // "Sun, 09 Mar 2008 16:05:07 GMT" RFC1123 String.Format("{0:s}", dt); // "2008-03-09T16:05:07" SortableDateTime String.Format("{0:u}", dt); // "2008-03-09 16:05:07Z" UniversalSortableDateTime
Specifier DateTimeFormatInfo property Pattern value (for en-US culture) tShortTimePattern h:mm ttdShortDatePattern M/d/yyyyTLongTimePattern h:mm:ss ttDLongDatePattern dddd, MMMM dd, yyyyf(combination of D and t) dddd, MMMM dd, yyyy h:mm ttFFullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss ttg(combination of d and t) M/d/yyyy h:mm ttG(combination of d and T) M/d/yyyy h:mm:ss ttm,MMonthDayPattern MMMM ddy,YYearMonthPattern MMMM, yyyyr,RRFC1123Pattern ddd, dd MMM yyyy HH':'mm':'ss 'GMT'(*)sSortableDateTimePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss(*)uUniversalSortableDateTimePattern yyyy'-'MM'-'dd HH':'mm':'ss'Z'(*)(*) = culture independent
Using C# 6 string interpolation format, the above line would be written this way:
// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
$"{dt:y yy yyy yyyy}"; // "8 08 008 2008" year
$"{dt:M MM MMM MMMM}"; // "3 03 Mar March" month
$"{dt:d dd ddd dddd}"; // "9 09 Sun Sunday" day
$"{dt:h hh H HH}"; // "4 04 16 16" hour 12/24
$"{dt:m mm}"; // "5 05" minute
$"{dt:s ss}"; // "7 07" second
$"{dt:f ff fff ffff}"; // "1 12 123 1230" sec.fraction
$"{dt:F FF FFF FFFF}"; // "1 12 123 123" without zeroes
$"{dt:t tt}"; // "P PM" A.M. or P.M.
$"{dt:z zz zzz}"; // "-6 -06 -06:00" time zone
// month/day numbers without/with leading zeroes
$"{dt:M/d/yyyy}"; // "3/9/2008"
$"{dt:MM/dd/yyyy}"; // "03/09/2008"
// day/month names
$"{dt:ddd, MMM d, yyyy}"; // "Sun, Mar 9, 2008"
$"{dt:dddd, MMMM d, yyyy}"; // "Sunday, March 9, 2008"
// two/four digit year
$"{dt:MM/dd/yy}"; // "03/09/08"
$"{dt:MM/dd/yyyy}"; // "03/09/2008"
Hi @IH
In SSRS, the now function can return a date value containing the current date and time according to the system. If you want to keep the locale setting, it's as simple as setting the Language expression to =User!Language in the report properties pane.
But using an expression in the report builder to keep the locale setting seems to be difficult to do to display the milliseconds of the date. SSRS does not display the milliseconds of the time by default. If you want to display it, using =FORMAT(NOW(), "MM/dd/yyyy hh:mm:ss.fff tt") seems to be a convenient method, but the time format in the local area will be destroyed.
What I can do is to separate the date from the time, the date is in the regional format, and the time is judged according to the value returned by the now function to determine whether there is "AM", "PM" in it, to choose different formats to display milliseconds.
=FormatDateTime(Now(), DateFormat.ShortDate)&" "&IIF(TimeOfDay().tostring.Contains("PM"),FORMAT(NOW(), "h:mm:ss.fff tt"),
iif(TimeOfDay().tostring.Contains("AM"),FORMAT(NOW(), "h:mm:ss.fff tt"),
FORMAT(NOW(), "HH:mm:ss.fff")))
Preview:
Hope this can help you.
Best regards,
Aniya
None of the suggestions would work. In the end, my solution was to just search the string and replace AM or PM with ".fff 'AM'" or ".fff 'PM'". This seems to work, but not ideal.
Use HH for 24 hour hours format:
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
Or the tt format specifier for the AM/PM part:
DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt")
Take a look at the custom Date and Time format strings documentation.
With C#6.0 you also have a new way of formatting date when using string interpolation e.g.
$"{DateTime.Now:yyyy-MM-dd HH:mm:ss}"
Can't say its any better, but it is slightly cleaner if including the formatted DateTime in a longer string.
More about string interpolation.