dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

🌟 Day.js 2.0 alpha preview is available

Open sxzz opened this issue 3 years ago β€’ 20 comments

v2 branch is here https://github.com/iamkun/dayjs/tree/next

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

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. 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 changed in the future) ~~import 'dayjs/plugin/isToday'~~ > import 'dayjs/dist/plugin/isToday'

Feature

  • supports ESM
  • rewrite using TypeScript

sxzz avatar May 02 '22 09:05 sxzz

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

benmccann avatar May 04 '22 03:05 benmccann

@benmccann 2.0 supports ESM now, you can try it https://unpkg.com/browse/@sxzz/[email protected]/dist/

sxzz avatar May 04 '22 03:05 sxzz

Thanks! Sorry for the noise. I didn't realize that as there wasn't any update on https://github.com/iamkun/dayjs/issues/1765

benmccann avatar May 04 '22 03:05 benmccann

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?

vhscom avatar May 04 '22 05:05 vhscom

@vhscom

As mentioned in issue content.

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

sxzz avatar May 04 '22 06:05 sxzz

ζ–‡ζ‘£εœ¨ε“ͺι‡Œε‘’οΌŸ

nieaowei avatar May 07 '22 15:05 nieaowei

ζ–‡ζ‘£εœ¨ε“ͺι‡Œε‘’οΌŸ Where is the documentation?

ζ—§ζ–‡ζ‘£δΉŸι€‚η”¨ Please refer to 1.x docs

sxzz avatar May 07 '22 15:05 sxzz

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 avatar May 09 '22 06:05 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)

  • 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" πŸ‘

BePo65 avatar May 09 '22 06:05 BePo65

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.

vhscom avatar May 09 '22 07:05 vhscom

@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, https://github.com/iamkun/dayjs/commit/f3088ea6143a62fdd11e23843c6d076caa4973da

  • 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/[email protected]/

  • 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 avatar May 09 '22 08:05 sxzz

@vhscom This issue is fixed in v2 branch.

image image

sxzz avatar May 09 '22 08:05 sxzz

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.

BePo65 avatar May 09 '22 09:05 BePo65

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

Bykiev avatar Jul 07 '22 18:07 Bykiev

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?

BePo65 avatar Jul 09 '22 14:07 BePo65

@BePo65 Fixed.

sxzz avatar Jul 10 '22 17:07 sxzz

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 πŸ˜„

BePo65 avatar Jul 12 '22 14:07 BePo65

I'd love if https://github.com/iamkun/dayjs/issues/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.

benmccann avatar Aug 04 '22 20:08 benmccann

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?

BePo65 avatar Aug 09 '22 08:08 BePo65

@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

Bykiev avatar Aug 18 '22 18:08 Bykiev

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 [email protected] everything works as expected.

ntnyq avatar Sep 23 '22 04:09 ntnyq

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

lchrennew avatar Sep 24 '22 18:09 lchrennew

The plugin is not yet implemented. Feel free to create a pr for that πŸ˜ƒ

BePo65 avatar Sep 25 '22 04:09 BePo65

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?

BePo65 avatar Sep 30 '22 04:09 BePo65

@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.

sxzz avatar Sep 30 '22 05:09 sxzz

@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?

Bykiev avatar Sep 30 '22 08:09 Bykiev

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

sxzz avatar Sep 30 '22 13:09 sxzz

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.

sxzz avatar Sep 30 '22 13:09 sxzz

image

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

donysukardi avatar Oct 13 '22 04:10 donysukardi

any changes> when will be new release?

websitevirtuoso avatar May 13 '23 06:05 websitevirtuoso