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

Is there a way to get both FP and UTC functionality? #3755

Open
daveyarwood opened this issue Apr 5, 2024 · 5 comments
Open

Is there a way to get both FP and UTC functionality? #3755

daveyarwood opened this issue Apr 5, 2024 · 5 comments

Comments

@daveyarwood
Copy link

daveyarwood commented Apr 5, 2024

I see that there is a date-fns/fp submodule and a date-fns/utc package, but is there a way to combine them?

@daveyarwood daveyarwood changed the title Is there a submodule that provides both FP and UTC functionality? Is there a way to get both FP and UTC functionality? Apr 5, 2024
@daveyarwood
Copy link
Author

I found this (old?) documentation which mentioned UTC being in development, and included instructions for how to import the package to use either FP, UTC, or both:

// The main submodule:
import addDays from 'date-fns/addDays'

// FP variation:
import addDays from 'date-fns/fp/addDays'

// UTC variation:
import addDays from 'date-fns/utc/addDays'

// Both FP and UTC:
import addDays from 'date-fns/fp/utc/addDays'

// With tree-shaking enabled:
import { addDays, format } from 'date-fns/fp'

But that seems like outdated information at this point, as the latest docs no longer mention UTC in that section.

After installing the latest date-fns, date-fns-tz, and @date-fns/utc packages, I tried requiring the combined FP/UTC package above, but it didn't work:

> const fp = require('date-fns/fp')
undefined
> const utc = require('@date-fns/utc')
undefined
> const fpAndUtc = require('@date-fns/fp/utc')
Uncaught Error: Cannot find module '@date-fns/fp/utc'
Require stack:
- <repl>
    at Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
    at Module._load (node:internal/modules/cjs/loader:901:27)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '<repl>' ]
}
> const fpAndUtcNoAtSymbol = require('date-fns/fp/utc')
Uncaught:
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './fp/utc' is not defined by "exports" in /Users/daveyarwood/code/spark/api/node_modules/date-fns/package.json
    at __node_internal_captureLargerStackTrace (node:internal/errors:497:5)
    at new NodeError (node:internal/errors:406:5)
    at exportsNotFound (node:internal/modules/esm/resolve:268:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:598:9)
    at resolveExports (node:internal/modules/cjs/loader:547:36)
    at Module._findPath (node:internal/modules/cjs/loader:621:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1034:27)
    at Module._load (node:internal/modules/cjs/loader:901:27)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Was support for this removed?

@fturmel
Copy link
Member

fturmel commented Apr 5, 2024

These old docs are no good. I think at some point in v2 there were undocumented UTC variations for some date-fns functions, but since v3 the way to go is to use the UTCDate package you originally mentioned.

So, it should work - with some caveats though:

  • The third party library date-fns-tz is not compatible with date-fns v3 yet, betas are out
  • UTCDate is in the package @date-fns/utc and needs to be installed as a separate dependency

Building off the example in the docs for FP use:

import { UTCDate } from '@date-fns/utc';
import { addYears, format } from 'date-fns/fp';
import { flow } from 'lodash-es';

const addFiveYears = addYears(5);
const formatToString = format('yyyy-MM-dd');

const dates = [new UTCDate(2017, 0, 1), new UTCDate(2017, 1, 11), new UTCDate(2017, 6, 2)];

const results = dates.map(flow(addFiveYears, formatToString));
console.log(results); // [ '2022-01-01', '2022-02-11', '2022-07-02' ]

@daveyarwood
Copy link
Author

startOfDay doesn't seem to work correctly with UTCDate instances:

> const { UTCDate } = require('@date-fns/utc')
undefined
> const { startOfDay } = require('date-fns/fp/index.js')
undefined
> new UTCDate("2024-03-29")
UTCDate 2024-03-29T00:00:00.000Z
> startOfDay(new UTCDate("2024-03-29"))
2024-03-28T04:00:00.000Z

I expected that to return 2024-03-28T00:00:00Z, but it offset it by 4 hours because I'm in the Eastern time zone.

@fturmel
Copy link
Member

fturmel commented Apr 8, 2024

You need to use UTCDate along with date-fns v3 for things to work properly (sorry if I wasn't clear). Looking at your logs I'm pretty sure it's v2 and the startOfDay() function returns a Date instead of UTCDate.

I double checked and it does work for me with v3.

import { UTCDate } from '@date-fns/utc';
import { startOfDay } from 'date-fns/fp';

const date = new UTCDate('2024-03-29');
console.log(date); // UTCDate 2024-03-29T00:00:00.000Z
console.log(startOfDay(date)); // UTCDate 2024-03-29T00:00:00.000Z

@daveyarwood
Copy link
Author

Thanks for the clarification! After updating the date-fns/fp package, it's working as expected 🎉

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

No branches or pull requests

2 participants