Skip to content

[Trubbel's Utilities] Time formats are inaccurate over longer times #375

Description

@AlcaDesign

I was looking at the output of /accountage and realized it's off by months. I looked up the function and the months and years use 30 days for a month. This is multiplied by 12 to get 360 days, but there are 365.25 days in a year (accounting for leap day every 4 years). My account was created in 2009 so the inaccuracy drifts from the real value by months.

For example, hardcoding the current date as 2026-07-18T07:41:55.171Z (Sat Jul 18 2026 02:41:55 GMT-0500) and testing against my account creation time 2009-08-10T17:32:19Z (Mon Aug 10 2009 12:32:19) it returns 17 years, 2 months, 5 days, 14 hours, 9 minutes and 36 seconds ago.

Original code:

const months = Math.floor(diffMs / (1e3 * 60 * 60 * 24 * 30)) % 12;
const years = Math.floor(diffMs / (1e3 * 60 * 60 * 24 * 30 * 12));

Modifying the function to the following results in a more accurate: 16 years, 11 months, 5 days, 14 hours, 9 minutes and 36 seconds ago

  const months = Math.floor(diffMs / (1e3 * 60 * 60 * 24 * (365.25 / 12))) % 12;
  const years = Math.floor(diffMs / (1e3 * 60 * 60 * 24 * 365.25));

You may even wish to update days which results in 16 years, 11 months, 6 days, 14 hours, 9 minutes and 36 seconds ago

const days = Math.floor(diffMs / (1e3 * 60 * 60 * 24) % (365.25 / 12));

These utility functions share a lot of code and should be simplified.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions