dayjs
dayjs copied to clipboard
π Day.js 2.0 alpha preview is available
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
Would you be open to a PR to convert the library to ESM for 2.0?
@benmccann 2.0 supports ESM now, you can try it https://unpkg.com/browse/@sxzz/[email protected]/dist/
Thanks! Sorry for the noise. I didn't realize that as there wasn't any update on https://github.com/iamkun/dayjs/issues/1765
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
As mentioned in issue content.
At this very early stage, some plugins and locales have not been implemented
ζζ‘£ε¨εͺιε’οΌ
ζζ‘£ε¨εͺιε’οΌ Where is the documentation?
ζ§ζζ‘£δΉιη¨ Please refer to 1.x docs
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 ;-)
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" π
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.
@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?
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
customParseFormatplugin.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. β€οΈ
@vhscom This issue is fixed in v2 branch.
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.
hey guys, thank you for your work, is any estimates when v2 will be finished and ready for use in production?
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 Fixed.
After a very simple plugin, I am now working on a more complex plugin - the utc plugin.
Currently I have 2 problems:
-
the utc plugin of v1 makes heavy use of internal fields and functions of the Dayjs object. For instance in the
$initfunction, it saves the$initfunction of Dayjs and calls it in his own$initfunction; but this is (IMO correctly) a private function.
Or accessing the$Lproperty (which is now called_locale).
Can you give me an idea, about a good way to handle these cases? -
The second problem is replacing a function already defined in Dayjs with a new signature (e.g.
utcOffset).
I tried the following code, but gotType '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 π
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.
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, 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
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.
I don't know where the duration plugin is...
The plugin is not yet implemented. Feel free to create a pr for that π
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 Currently, we plan to release next branch as v3, because refactoring all code is a really heavy task. v2 will only fix supporting ESM.
@BePo65 Currently, we plan to release
nextbranch 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 it has been released as 2.0.0-alpha.4. When does it get stable? I don't know. /cc @iamkun
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.
the type for timezone plugin seems to be incorrect.. it's not registering setDefault function
any changes> when will be new release?