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

🌟 Day.js 2.0 alpha preview is available #1879

Closed
sxzz opened this issue May 2, 2022 · 40 comments
Closed

🌟 Day.js 2.0 alpha preview is available #1879

sxzz opened this issue May 2, 2022 · 40 comments

Comments

@sxzz
Copy link

sxzz commented May 2, 2022

The v2 branch is here #2066

If you have issues with v2, please comment on this issue. Please do not comment on irrelevant content.

Changelog

  • supports ESM
Outdated

The information is outdated, we're planning them released when v3.0.

Changelog

2.0.0-alpha.0

⚠️ At this very early stage, some plugins and locales have not been implemented. (PR welcomed)
⚠️ v2 has been rewritten. The compatibility of any private API is not guaranteed.

Breaking Change

  • dayjs.locale(preset, object, isLocal) => dayjs.locale(preset, isLocal, object)
  • drop support of node < 14.19.0
  • importing plugins or locale (maybe will be changed in the future)
    import 'dayjs/plugin/isToday' > import 'dayjs/dist/plugin/isToday'

Feature

  • supports ESM
  • rewrite using TypeScript
@sxzz sxzz pinned this issue May 2, 2022
@iamkun iamkun changed the title 🌟 Dayjs 2 Has Been Released! 🌟 Day.js 2.0 alpha preview is available May 2, 2022
@benmccann
Copy link

benmccann commented May 4, 2022

Would you be open to a PR to convert the library to ESM for 2.0?

@sxzz
Copy link
Author

sxzz commented May 4, 2022

@benmccann 2.0 supports ESM now, you can try it
https://unpkg.com/browse/@sxzz/dayjs@2.0.0-alpha.2/dist/

@benmccann
Copy link

Thanks! Sorry for the noise. I didn't realize that as there wasn't any update on #1765

@vhscom
Copy link

vhscom commented May 4, 2022

I bumped to the v2 alpha (build 2) and it looks like some plugins are missing from the packaged bundles. Things I was looking for are weekOfYear, isBetween and isSameOrAfter though the /dist/plugins directory doesn't seem to include them. Is that a packaging error or are those being rewritten?

@sxzz
Copy link
Author

sxzz commented May 4, 2022

@vhscom

As mentioned in issue content.

At this very early stage, some plugins and locales have not been implemented

@nieaowei
Copy link

nieaowei commented May 7, 2022

文档在哪里呢?

@sxzz
Copy link
Author

sxzz commented May 7, 2022

文档在哪里呢?
Where is the documentation?

旧文档也适用
Please refer to 1.x docs

@BePo65
Copy link
Contributor

BePo65 commented May 9, 2022

Perhaps it is a good idea to stay with English in this repository so that everybody can follow the discussions (but perhaps it is a good idea to use it to improve our knowledge of foreign languages - just kidding ;-)

@BePo65
Copy link
Contributor

BePo65 commented May 9, 2022

IMHO it is a good idea to redo the code base for dayjs because this way we have a chance to improve the usability for typescript and add esm support.
I build a minimal angular 13 project to get accustomed to the new version and here are my first findings:

  • 'utility-types' is a 'dependencies' (not a 'devDependencies') used in 'units' (resulting in a "cannot find module 'utility-types'" when building my angular app)

  • the dist folder does not contain a 'package.json' file after building dayjs.
    It is a good practice - or a requirement? - to include a package.json to a project); even without a dependency, the generated code that will be published to npmjs, should contain a package.json to give information about the project.

  • to build version 2, I fetched the next branch, deleted the package-lock.json and the node_modules folder and ran 'npm i'.
    Running 'lint' I got 745 warnings, lots of them stating "Delete eslintprettier/prettier".
    OK, this is an alpha version, so this is not a problem, but the character mentioned in the warning was not an ASCII character (;-), but always at the end of all lines in the referenced files (e.g. in dayjs.ts). Just as I am curious: is this an uncommon line endings character?
    Unluckily I did not find the sources of @sxzz/eslint-config-prettier and @sxzz/eslint-config-ts and their package.json did not contain a 'repository' tag (which is at least a good practice). @sxzz perhaps you could add this tag to your eslint packages?

As a next step, I will add more examples to my test project (e.g. using locales).

If you need a helping hand, I could contribute e.g. one of the plugins. As I am currently working on issue #1596, perhaps I could "migrate" the customParseFormat plugin.

Anyway, thanks for starting this "issue" 👍

@vhscom
Copy link

vhscom commented May 9, 2022

If reworking the code, a rethink on how plugins are included would be nice (especially since only two or three have been implemented). Currently my static analysis tools aren't able to determine when a particular plug-in is no longer used because each is immediately referenced by name within my codebase, e.g.:

import dayjs, { type Dayjs } from 'dayjs';
import weekOfYear from 'dayjs/plugin/weekOfYear.js';
import isBetween from 'dayjs/plugin/isBetween.js';
dayjs.extend(weekOfYear);
dayjs.extend(isBetween);

If I stop using weekOfYear or isBetween my static analysis tools won't tell me.

I realize the way the plug-ins are architected this goal is not easily achievable but perhaps someone knows a technique which might help address this.

@sxzz
Copy link
Author

sxzz commented May 9, 2022

@BePo65

IMHO it is a good idea to redo the code base for dayjs because this way we have a chance to improve the usability for typescript and add esm support. I build a minimal angular 13 project to get accustomed to the new version and here are my first findings:

  • 'utility-types' is a 'dependencies' (not a 'devDependencies') used in 'units' (resulting in a "cannot find module 'utility-types'" when building my angular app)

Fixed, f3088ea

  • the dist folder does not contain a 'package.json' file after building dayjs.
    It is a good practice - or a requirement? - to include a package.json to a project); even without a dependency, the generated code that will be published to npmjs, should contain a package.json to give information about the project.

It's a good practice, i think. The dist folder only contains bundle code, and other files will located in root folder.

You can see the bundle files here. https://unpkg.com/browse/dayjs@2.0.0-alpha.2/

  • to build version 2, I fetched the next branch, deleted the package-lock.json and the node_modules folder and ran 'npm i'.
    Running 'lint' I got 745 warnings, lots of them stating "Delete eslintprettier/prettier".
    OK, this is an alpha version, so this is not a problem, but the character mentioned in the warning was not an ASCII character (;-), but always at the end of all lines in the referenced files (e.g. in dayjs.ts). Just as I am curious: is this an uncommon line endings character?

dayjs 2 uses pnpm now. Please remove node_modules and try pnpm install.

Unluckily I did not find the sources of @sxzz/eslint-config-prettier and @sxzz/eslint-config-ts and their package.json did not contain a 'repository' tag (which is at least a good practice). @sxzz perhaps you could add this tag to your eslint packages?

Source code.

OK, I will add it later.

As a next step, I will add more examples to my test project (e.g. using locales).

If you need a helping hand, I could contribute e.g. one of the plugins. As I am currently working on issue #1596, perhaps I could "migrate" the customParseFormat plugin.

Anyway, thanks for starting this "issue" 👍

That's great. But I don't have a lot of time to focus on open source at the moment, so your PR may not be reviewed in time, but I'll do it as soon as I can. ❤️

@sxzz
Copy link
Author

sxzz commented May 9, 2022

@vhscom
This issue is fixed in v2 branch.

image

image

@BePo65
Copy link
Contributor

BePo65 commented May 9, 2022

That's great. But I don't have a lot of time to focus on open source at the moment, so your PR may not be reviewed in time, but I'll do it as soon as I can.

But anyway that is what I call a real fast answer 👍

So I will create a pr for issue #1596 for dayjs v2.0 - will take a few days.

@Bykiev
Copy link

Bykiev commented Jul 7, 2022

hey guys, thank you for your work, is any estimates when v2 will be finished and ready for use in production?

@BePo65
Copy link
Contributor

BePo65 commented Jul 9, 2022

Still struggling with the baseline: when building the project with pnpm run build, I get the following error:

error TS5069: Option 'emitDeclarationOnly' cannot be specified without specifying option 'declaration'
or option 'composite'.

although the compilerOptions in tsconfig.src.json and tsconfig.node.json contain "declaration": true.

Any idea why this happens or am I the only one with effect?

@sxzz
Copy link
Author

sxzz commented Jul 10, 2022

@BePo65 Fixed.

@BePo65
Copy link
Contributor

BePo65 commented Jul 12, 2022

After a very simple plugin, I am now working on a more complex plugin - the utc plugin.

Currently I have 2 problems:

  1. the utc plugin of v1 makes heavy use of internal fields and functions of the Dayjs object. For instance in the $init function, it saves the $init function of Dayjs and calls it in his own $init function; but this is (IMO correctly) a private function.
    Or accessing the $L property (which is now called _locale).
    Can you give me an idea, about a good way to handle these cases?

  2. The second problem is replacing a function already defined in Dayjs with a new signature (e.g. utcOffset).
    I tried the following code, but got Type 'utcOffsetType' is not assignable to type '() => number'

export type utcOffsetType = (offset: number | string, keepLocalTime?: boolean) => Dayjs
:
declare module '../types' {
  export interface Extend {
    utcOffset: utcOffsetType
  }
}
:
const plugin: Plugin = (cls, fn) => {
  const utcOffset: utcOffsetType = function (
    offset: number | string,
    keepLocalTime?: boolean
  ): Dayjs {
  // do something usefull here
}
:
  cls.prototype.utcOffset = utcOffset
}

Any help appreciated 😄

@benmccann
Copy link

I'd love if #498 could be implemented on the next branch to get rid of the need for locale files in the same way that Luxon supports all languages simply using the built-in browser support.

@BePo65
Copy link
Contributor

BePo65 commented Aug 9, 2022

I am currently working on the utc and customParseFormat plugins as examples for more complex plugins.
As IMHO the 'core' dayjs module should not contain code that is exclusively needed by a plugin, I was taking a more detailed look at the 'core' code.

During that investigation I noticed that the manipulate.test.js from dayjs 1.x was missing. So I added it.

But then I stumbled over the fact that in the definition of add and subtract the 'D' unit was removed from the allowed units.
When I look at the units.ts, then in 'export const units' the definition D: UNIT_DATE is in the position for 'day of month and a few lines below we have d: UNIT_DAY /** Day of week */. In the documentation it looks like 'd' is 'day of month'.

Are the letters 'd' and 'D' just swapped in the definition of Units or do I simply not understand the unit definitions (they are quite complex and understanding the use of the different types requires some time :-).

So @iamkun can you help me understand the add / subtract signature or the definition of the units?

@Bykiev
Copy link

Bykiev commented Aug 18, 2022

@BePo65, as dayjs is replacement for moment D should be day of month and d is day of week
https://momentjs.com/docs/#/displaying/format/
Units for add/subtract methods seems to be have different meaning and d is just a day

@ntnyq
Copy link

ntnyq commented Sep 23, 2022

Repro repo

Run pnpm start

console.log(dayjs().format(`YYYY-MM-DD`)) // 2022-09-23
console.log(dayjs().subtract(1, 'day').format(`YYYY-MM-DD`)) // 2022-09-22

The outputs are as expected.

Run pnpm test

console.log(dayjs().format(`YYYY-MM-DD`))) // 2022-09-23
console.log(dayjs().subtract(1, 'day').format(`YYYY-MM-DD`))) // 2022-09-23

Expression above got the same output which is weired.

Instead with dayjs@1.x everything works as expected.

@lchrennew
Copy link

I don't know where the duration plugin is...

@BePo65
Copy link
Contributor

BePo65 commented Sep 25, 2022

The plugin is not yet implemented. Feel free to create a pr for that 😃

@BePo65
Copy link
Contributor

BePo65 commented Sep 30, 2022

Urgent question about the base branch for dayjs2.0

@sxzz I am currently working on implementing the more complicated plugins for dayjs 2.0 ('utc', 'CustomParseFormat'); my base for this code is the branch 'next'.

Now I noticed that the name of branches changed; there is even a new branch '2.0' (using jest and not vitest).
Before I invest to much time: can you tell me,what branch contributions to dayjs 2.0 should be based on?

@sxzz
Copy link
Author

sxzz commented Sep 30, 2022

@BePo65 Currently, we plan to release next branch as v3, because refactoring all code is a really heavy task. v2 will only fix supporting ESM.

@Bykiev
Copy link

Bykiev commented Sep 30, 2022

@BePo65 Currently, we plan to release next branch as v3, because refactoring all code is a really heavy task. v2 will only fix supporting ESM.

any news when v2 will be available?

@sxzz
Copy link
Author

sxzz commented Sep 30, 2022

@Bykiev it has been released as 2.0.0-alpha.4. When does it get stable? I don't know. /cc @iamkun

@sxzz
Copy link
Author

sxzz commented Sep 30, 2022

dayjs 2 will be experimental in Element Plus v3 branch. When it gets stable, then we will consider making dayjs 2 into the stable branch.

@donysukardi
Copy link

image

the type for timezone plugin seems to be incorrect.. it's not registering setDefault function

@websitevirtuoso
Copy link

any changes> when will be new release?

@websitevirtuoso
Copy link

Last change in v2 has been made 8 month ago. I think no one is working on v2.

@MCFreddie777
Copy link

Bump

@nestle49
Copy link

Any news?

@nestle49
Copy link

nestle49 commented Nov 9, 2023

R u alive?

@andorfermichael
Copy link

I hope this will continue ...

@BePo65
Copy link
Contributor

BePo65 commented Dec 8, 2023

Is there somebody out there, who is willing to take over this project? I will gladly do my part to help improve dayjs, but only if there is a chance that prs will be reviewed and merged / rejected in a timely manner. What do you think?

@nestle49
Copy link

@sxzz does it make sense to wait for version 2?

@damianobarbati
Copy link

any news on this? :(

@muuvmuuv
Copy link

Sadly, I switched to date-fns (it uses native Date as input which is kinda nice), since Dayjs was the last CommonJS lib and I really wanted to be full ESM. Would love to go back since I love the API on dayjs (and it wasn't that difficult to migrate).

@lordvcs
Copy link

lordvcs commented Mar 2, 2024

bump

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