# Replace Moment.js With Day.js in an Express API

Yes. An agent replaces every Moment.js call with Day.js, checks that token expiry times come out identical, and opens a pull request with the suite passing.

[Run this on Rush](https://cloud.getrush.ai/run/migrate-moment-to-dayjs/moment/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent maps where Moment is used and which of its methods the code depends on. In an API like hagopj13/node-express-boilerplate the answer is narrow. `src/services/token.service.js` requires `moment` and uses it for JWT timing: `moment().unix()` for `iat`, `moment().add(n, 'minutes')` and `moment().add(n, 'days')` for the four expiry times, then `expires.unix()` in the payload and `expires.toDate()` for the stored token. Two test files use the same calls plus `subtract(1, 'minutes')` to build expired tokens: `tests/integration/auth.test.js` on 22 lines and `tests/fixtures/token.fixture.js` on two. `package.json` asks for `moment` `^2.24.0` and `yarn.lock` resolves it to 2.29.1.

Every one of those methods exists in Day.js core, so no plugin is needed. The risk is in arithmetic and mutation, and the agent tests both. The durations come from environment variables that the Joi schema in `src/config/config.js` types as numbers, so a fraction is legal input. For a fixed start time, Moment 2.29.1 and Day.js 1.11.23 give the same result for 0.5 minutes, 10 minutes, 30 days and 1.5 days; both treat 1.5 days as two. On mutation they differ: `moment().add()` changes the object and returns it, `dayjs().add()` returns a new one. The agent reads each call site and confirms that every result is assigned where it is created, so nothing here relies on the old object changing.

The dependency change has a twist that `package.json` does not show. `yarn why moment` reports two reasons the package is installed: the app's own dependency, and pm2, through `cron` and `moment-timezone`. Removing the app's entry leaves Moment 2.29.1 in `node_modules`, and `require('moment')` keeps resolving. In the other direction, `dayjs` is already there at 1.8.36 as a dependency of pm2, so the new `require('dayjs')` would work even if the agent forgot to add the package. It adds `dayjs` to `dependencies` explicitly, so the app owns the version it runs, and adds an ESLint `no-restricted-modules` entry for `moment` to `.eslintrc.json`, because nothing else would stop the old import coming back.

Then it rewrites the three files, updates the `{Moment}` JSDoc types on `generateToken` and `saveToken`, and runs the tests against a MongoDB it installs in the sandbox. When this page was written the unchanged repository passed 113 tests on Node 22, and the same 113 passed after the swap.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Replace Moment.js with Day.js in this Express API. Read package.json, yarn.lock, src/services/token.service.js, src/config/config.js and every test or fixture that requires moment, and list each Moment method the code calls. For each one, say whether Day.js core has it or which plugin it needs. Before editing, write a short script that compares Moment and Day.js on the arithmetic this code does, including fractional minutes and days since the durations come from environment variables, and on whether add() mutates; then check every call site for reliance on mutation. Run yarn why moment and yarn why dayjs and explain what each says about where the package comes from. Add dayjs as a direct dependency, remove moment from package.json, rewrite the calls and the JSDoc types, and add a lint rule that rejects require('moment'). There is no Docker here: install a MongoDB server, start mongod on 127.0.0.1:27017, copy .env.example to .env and run yarn test before and after. Run yarn lint before and after and report any error that was already there separately from anything you introduced. Open a pull request with the output of every command.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that changes `package.json`, `yarn.lock`, `.eslintrc.json`, `src/services/token.service.js` and the two test files. The description lists the Moment methods found and their Day.js equivalents, prints the comparison script with its output, and quotes `yarn why` for both packages. It ends with the Jest summary before and after, and the lint output in two parts: on the unchanged repository `yarn lint` already fails with seven `prettier/prettier` errors in `src/middlewares/auth.js`, a file this change does not touch, and the description keeps that apart from the new rule.

The reviewer reads the comparison first, because token lifetime is security behaviour: an access token that lives longer or shorter than configured is a bug no test here would catch unless the numbers match. They check that the script covers the values their own environment sets. They also note what did not change: Moment is still on disk through pm2, so the install is not smaller, and the pull request says so plainly. If dropping it entirely is the goal, that is a pm2 upgrade and a separate change.

## Run it on every Linear issue labelled tech-debt

Library swaps like this usually sit in a backlog as small issues. Connect Linear under Settings → Connections, then add an automation on the Project with a `linear` trigger on `linear.issue.labeled`, the label your team uses for this kind of work, and a saved prompt that tells the agent to do the migration the issue names and prove it with a comparison script and the test suite. The issue is added after the prompt as event text; the prompt stays the only instruction.

The trigger fires only when a person in your Rush org adds the label, and never when an app integration does. Each run opens its own pull request, so a week's worth of labelled issues becomes a list of reviews.

## Why run it on Rush?

Replacing a date library is mostly a question of whether two implementations agree on the inputs your code feeds them. That is answered by running both, which the agent does in a sandbox with both packages installed at the locked versions, a database for the integration tests, and nothing left over from another project. The comparison script, its output and the test results are kept with the run, and the pull request quotes them.

The dependency findings also come from commands, not from reading manifests. That Moment stays installed through pm2, and that Day.js was importable before it was declared, are facts about the resolved tree. A person editing `package.json` in an editor would learn the first one later, when a dependency listing still showed Moment, and might never learn the second.

## Frequently asked questions

### Does Day.js need plugins to replace Moment here?

No. The code uses `add`, `subtract`, `unix` and `toDate`, all of which are in Day.js core. Plugins come into it for things this repository does not do, such as time zones, durations, relative time or custom parse formats; the agent lists the methods it found so you can see that none of those apply.

### Will existing tokens stop working after the change?

No. A token carries `iat` and `exp` as Unix seconds, and both libraries produce the same integers for the same clock and duration. Tokens issued before the change verify exactly as before, since verification reads the numbers in the token and not the library that wrote them.

### Why is Moment still in node_modules afterwards?

Because another package depends on it. In this repository pm2 pulls in `cron`, which needs `moment-timezone`, which needs `moment`. Your code no longer imports it, and the lint rule keeps it that way, but removing it from disk means changing or upgrading pm2.

### Is the mutable versus immutable difference a real risk?

It can be. Code that calls `m.add(1, 'day')` on a shared Moment object and ignores the return value silently stops working with Day.js, because the original is no longer changed. Here every `add` and `subtract` result is assigned directly to a new constant, which the agent verifies call by call before rewriting.

## More Moment.js jobs

- [Fix Moment's "Value Provided Is Not in a Recognized RFC2822 or ISO Format" Warning](https://getrush.ai/run/fix-moment-value-provided-is-not-in-a-recognized-format-warning/moment/)

## More jobs like this: Turn a Linear label into a PR

- [Replace pytz With zoneinfo in a Python Scraper](https://getrush.ai/run/migrate-pytz-to-zoneinfo/pytz/)
- [Migrate From querystring to URLSearchParams in a Node.js API Client](https://getrush.ai/run/migrate-querystring-to-urlsearchparams/querystring/)
- [Migrate From Requests to HTTPX in a Python Project](https://getrush.ai/run/migrate-requests-to-httpx/requests/)
- [Replace shortid With nanoid in a React App](https://getrush.ai/run/migrate-shortid-to-nanoid/shortid/)
- [Migrate a Blazor App from SQL Server to PostgreSQL](https://getrush.ai/run/migrate-sql-server-to-postgres/blazor/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
