2

I am getting dates from 2 different API endpoints and saving them to MongoDB using Mongoose (used date in the schema).

Here is an example of the 2 dates in the database. Using dayjs, one of them works correctly and the other one shows in 2 hours instead of X minutes ago for example.

correct:   2022-01-11T16:05:05.000+00:00
incorrect: 2022-01-11T17:41:50.000+00:00

import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
dayjs.extend(relativeTime);

{dayjs(data.paperDate).fromNow()} // returns 'in 2 hours'

Is there something wrong with the date marked as 'incorrect' ?

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
Waterfall
  • 584
  • 1
  • 5
  • 15
  • The question is quite unclear. What is "works correctly"? What is an example of something correct and one incorrect? A date is a date, and it will always "work correctly" if no error is raised when creating the Date object or persisting in the database. Tried to reproduce a minimal example [here](https://codesandbox.io/s/peaceful-snow-39pvx?file=/src/index.js), I don't see any problem. Also, [here](https://day.js.org/docs/en/display/from-now#list-of-breakdown-range) are the breakdown ranges, if that's your question. Perhaps you want to look at [difference](https://shorturl.at/msS26). – Gaëtan Boyals Jan 11 '22 at 18:34
  • Do you insist to use day.js? I prefer [luxon](https://moment.github.io/luxon/#/math?id=casual-vs-longterm-conversion-accuracy), because along with others, [ISO-8601](https://moment.github.io/luxon/#/parsing?id=iso-8601) support is much better than in day.js. According method would be [Duration.toHuman](https://moment.github.io/luxon/api-docs/index.html#durationtohuman) – Wernfried Domscheit Jan 11 '22 at 20:14
  • @WernfriedDomscheit the main reason I wanted to use dayjs was because of its small size. It is 623kb. I looked at Luxon which I would have tried but in comparison it is 3.72mb! – Waterfall Jan 12 '22 at 07:27
  • @GaëtanBoyals 'works correctly' means that the date in the database when using dayjs on the frontend provides the desired result ie. returns for example, '25 minutes ago' where as the 'incorrect' value returns 'in 2 hours' which is obviously incorrect. Both dates are using the same dayjs code on the frontend but with different results so it seems like the the date saved into the database is 'incorrect'. – Waterfall Jan 12 '22 at 07:29
  • 1
    I see. Did you check the CodeSandBox I linked? Also, the "in" as in "in 2 hours" makes me think you perhaps mistakenly used `toNow()` instead of `fromNow()`. But without the full excerpt of code, that's a bit hard to determine what is really going on. I wasn't able to reproduce the issue based on the data you gave in your example unfortunately... – Gaëtan Boyals Jan 12 '22 at 08:24
  • When you install luxon then you get the source-code and packages for amd, cjs-browser, es6, global, node. You need only the "node" package which is 209k. – Wernfried Domscheit Jan 12 '22 at 09:01
  • So, after all of that, it seems like the issue was the rss to json parser package I was using that was messing up the one particular date. When I tried another then everything works fine. Thanks for all the suggestions though, good to learn about Luxon as well! – Waterfall Jan 12 '22 at 13:43

1 Answers1

0

see https://momentjs.com/docs/

moment(new Date(2021)).fromNow(); // a year ago
moment(new Date(2021)).from(new Date(2012))
Kareem Adel
  • 371
  • 2
  • 10