date-fns format is returning wrong data (Eg: instead of 2020, I'm getting 52772)

I'm using date-fns/format utility to format the date in my application. I don't know what I'm doing wrong. date-fns just not returning the correct value. For example, instead of 24 Nov 2020, I'm getting 27 May 52869. Below is my implementation,

function getTimeText(){
const convertedDate = fromUnixTime(date);
return format(convertedDate, "'Yesterday at' h:m a");
}

As you can see above, I'm passing UNIX seconds to date-fns fromUnixTime function. Maybe something wrong with that? No idea.

Can anyone give me a hand?

1 Answers

I checked your code. The issue with your code is you're not dividing the Unix timestamp by 1000. In your code, replace the below line,

function getTimeText(){
const dateInMilliseconds = new Date().getTime() / 1000;
const convertedDate = fromUnixTime(date);
return format(convertedDate, "'Yesterday at' h:m a");
}

Now it should work. If still facing the issue, let me know. I'm happy to help you.

0
Your Answer

Do you know any other way to solve the above problem? If so, write your answer to help the community.

Write here