1

I am confused about JavaScript's Date object and its getTime() method. From: Minimum and maximum date

It says that the actual max and min. number of milliseconds (ms) representable by a JS Date object is (-/+)8.640.000.000.000.000 ms since 01 January, 1970 UTC.

However, 8.640.000.000.000.000 requires 53 bits.

On the other, hand, all JS numbers are 64-bit floats with only 52 fractional bits (excluding the sign bit).

How can JS therefore store the result of Date.getTime() when Date is either the max or the min date value? It must use one of the exponent bits to do this, right?

Does that also mean that converting a JS Date (via its number of ms) to int64 (e.g. for use in Qt) is not strictly possible?

  • 3
    [52 fractional bits is the same as 53 significant bits](https://stackoverflow.com/a/13543600/5267751). "Since we are using binary (only 0 and 1), one bit in the mantissa is implicitly 1 (both float and double use this trick) when the number is non-zero." – user202729 Aug 05 '18 at 03:31
  • 1
    Planning more than 100k years ahead? – dtech Aug 05 '18 at 13:44
  • Thanks. Forgot about the normalization which makes the msb always 1 (for non-zero numbers) and can therefore be made implicit, effectively giving an extra bit... –  Aug 06 '18 at 03:40

1 Answers1

0

JavaScript represents numbers using IEEE-754 double-precision (64 bit) format. it gives you 53 bits precision. JS internally stores as 64-bit floating point numbers and JavaScript dates are internally stored as milliseconds since epoch.You can use getTime() method of JavaScript . I will return epoch value . This epoch value can be used in Qt as epoch value or create date from this epoch value using

fromMSecsSinceEpoch(qint64 msecs)

Masthan
  • 727
  • 1
  • 8
  • 29