Get Datetime.Now with Milliseconds Precision

.NET datetime Millisecond precision issue when converting from string to datetime

According to Custom Date and Time Format Strings docs, 7 is maximum supported digits of second fraction.

Return time.now with millisecond precision

Use fff instead of FFF

time.ToString("HH:mm:ss.fff");

Example

var now = new DateTime(2017,10,18,12,16,16, 10);
Console.WriteLine("What you want {0:HH:mm:ss.fff}", now);
Console.WriteLine("WHat you have {0:HH:mm:ss.FFF}", now);

Produces output

What you want 12:16:16.010
WHat you have 12:16:16.01

For more format string options see Custom Date and Time Format Strings

Get time in milliseconds using C#

Use the Stopwatch class.

Provides a set of methods and
properties that you can use to
accurately measure elapsed time.

There is some good info on implementing it here:

Performance Tests: Precise Run Time Measurements with System.Diagnostics.Stopwatch

C# DateTime.Now precision

Why would DateTime.Now be made less precise than what most CPU clocks could handle?

A good clock should be both precise and accurate; those are different. As the old joke goes, a stopped clock is exactly accurate twice a day, a clock a minute slow is never accurate at any time. But the clock a minute slow is always precise to the nearest minute, whereas a stopped clock has no useful precision at all.

Why should the DateTime be precise to, say a microsecond when it cannot possibly be accurate to the microsecond? Most people do not have any source for official time signals that are accurate to the microsecond. Therefore giving six digits after the decimal place of precision, the last five of which are garbage would be lying.

Remember, the purpose of DateTime is to represent a date and time. High-precision timings is not at all the purpose of DateTime; as you note, that's the purpose of StopWatch. The purpose of DateTime is to represent a date and time for purposes like displaying the current time to the user, computing the number of days until next Tuesday, and so on.

In short, "what time is it?" and "how long did that take?" are completely different questions; don't use a tool designed to answer one question to answer the other.

Thanks for the question; this will make a good blog article! :-)

datetime.now() sometimes dont return milliseconds

This is according to spec.

Return a string representing the date and time in ISO 8601 format:

YYYY-MM-DDTHH:MM:SS.ffffff, if microsecond is not 0

YYYY-MM-DDTHH:MM:SS, if microsecond is 0

If you want to precisely control the formatting, you should use datetime.strftime.

As MrFuppes mentions, you can also use the datetime.isoformat function but pass timespec=microseconds in order to always show the microseconds.

Timestamp with a millisecond precision: How to save them in MySQL

You need to be at MySQL version 5.6.4 or later to declare columns with fractional-second time datatypes. Not sure you have the right version? Try SELECT NOW(3). If you get an error, you don't have the right version.

For example, DATETIME(3) will give you millisecond resolution in your timestamps, and TIMESTAMP(6) will give you microsecond resolution on a *nix-style timestamp.

Read this: https://dev.mysql.com/doc/refman/8.0/en/fractional-seconds.html

NOW(3) will give you the present time from your MySQL server's operating system with millisecond precision.

If you have a number of milliseconds since the Unix epoch, try this to get a DATETIME(3) value

FROM_UNIXTIME(ms * 0.001)

Javascript timestamps, for example, are represented in milliseconds since the Unix epoch.

(Notice that MySQL internal fractional arithmetic, like * 0.001, is always handled as IEEE754 double precision floating point, so it's unlikely you'll lose precision before the Sun becomes a white dwarf star.)

If you're using an older version of MySQL and you need subsecond time precision, your best path is to upgrade. Anything else will force you into doing messy workarounds.

If, for some reason you can't upgrade, you could consider using BIGINT or DOUBLE columns to store Javascript timestamps as if they were numbers. FROM_UNIXTIME(col * 0.001) will still work OK. If you need the current time to store in such a column, you could use UNIX_TIMESTAMP() * 1000

How do I get the current time in milliseconds in Python?

Using time.time():

import time

def current_milli_time():
return round(time.time() * 1000)

Then:

>>> current_milli_time()
1378761833768

DateTime.Now differs a millisecond

This is most likely an issue with the precision of DB's datetime column: it does not have enough accuracy to store the time up to a millisecond. When you use datetime, the time portion is rounded to increments of .000, .003, or .007 seconds.

Switching column's type to datetime2 should help, because its resolution is 100 nanoseconds.



Related Topics



Leave a reply



Submit