Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fromUnixTime ignored milliseconds #1917

Open
dd-han opened this issue Aug 25, 2020 · 5 comments
Open

fromUnixTime ignored milliseconds #1917

dd-han opened this issue Aug 25, 2020 · 5 comments
Milestone

Comments

@dd-han
Copy link

dd-han commented Aug 25, 2020

Hi

When I using date-fns convert Unix Timestamp to JavaScript Date Object. But milliseconds seem to be ignored, code:

datefns = require('date-fns')
datefns.fromUnixTime(0.1).getMilliseconds()
// output: 0
// but expect output: 100

https://github.com/date-fns/date-fns/blob/master/src/fromUnixTime/index.js#L29

@minitesh
Copy link
Contributor

minitesh commented Oct 1, 2020

@dd-han
It is because fromUnixTime() expects timestamp in seconds and converts it into milliseconds by multiplying timestamp by 1000.
So in your case 0.1 gets converted to 100 (i.e. 0.1 * 1000 => 100) so you are getting milliseconds as 0.

But even JavaScript Date will also give you the same result.

const d = new Date(0.1); // Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)
d.getMilliseconds(); // 0

@leshakoss @kossnocorp shouldn't the function fromUnixTime() providing support for timestamp in milliseconds along with seconds rather than expecting argument in seconds and converting it to milliseconds?

@fturmel
Copy link
Member

fturmel commented Dec 30, 2021

shouldn't the function fromUnixTime() providing support for timestamp in milliseconds along with seconds rather than expecting argument in seconds and converting it to milliseconds

The Date constructor already supports timestamps in ms, so I think this request doesn't make much sense.

const date = new Date(100)
console.log(date.toISOString()) // '1970-01-01T00:00:00.100Z'
console.log(date.getMilliseconds()) // 100, the ms component only of the date created
console.log(date.getTime()) // 100, ms since epoch

new Date(0.1) would be a tenth of a millisecond after ECMAScript epoch, which is a lower resolution than the Date object supports. You'll just get the same result as new Date(0) which is 1970-01-01T00:00:00.000Z.

@fturmel
Copy link
Member

fturmel commented Dec 30, 2021

Oh hold on, let me re-open this. Do you mean that the fromUnixTime function shouldn't drop the decimals, that way they could be used to represent milliseconds?

Essentially, these two lines should match instead of truncating the decimal values?

import { fromUnixTime } from "date-fns";

const ms = 1640888727872;
const sec = ms / 1000;

console.log(new Date(ms).toISOString());      // 2021-12-30T18:25:27.872Z
console.log(fromUnixTime(sec).toISOString()); // 2021-12-30T18:25:27.000Z

There is a specific unit test for this, so it seems like it was intentional and behaving this way since it was introduced by #937.

it('discards decimal values', function () {
const result = fromUnixTime(1330515499.75)
assert(result.getTime() === 1330515499000)
})

I assume it would be treated as a breaking change, but maybe this could be considered for v3? @tan75

@fturmel fturmel reopened this Dec 30, 2021
@leshakoss leshakoss added this to the v3 milestone Jan 4, 2022
@tan75
Copy link
Contributor

tan75 commented Jan 4, 2022

hi all,

it is currently open for discussion.

@italosantana
Copy link

@tan75 some update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants