Dayjs: Constructor with custom format

Created on 12 May 2018  路  13Comments  路  Source: iamkun/dayjs

moment.js has wide usable feature to make date from custom strings. For example:
moment('2018-02-01', 'YYYY-DD-MM')
or
moment('02-2018-01', 'DD-YYYY-MM');

Do we want to support this feature in our core? I'm not sure how to implement it in minimalist way, but I think we need to rewrite .format function somehow to avoid copy-paste of code. If you will be agree, guys, that we need to have this feature - I'll think about it.

Most helpful comment

@iamkun Yep using ISO would format is a great advice, but some (me :D) have to deal with data from other sources which doesn't support the ISO date format.

All 13 comments

maybe some one need this feature, but still I recommend using ISO date format always.

@iamkun Yep using ISO would format is a great advice, but some (me :D) have to deal with data from other sources which doesn't support the ISO date format.

Is someone working in that feature (if not I can try to do it), as I try to get rid of moment in https://www.jhipster.tech/ which is part of the react validation lib which relies on that specific moment api (moment('2018-02-01', 'YYYY-DD-MM')) to check if the date entered in a component is a valid date (and conforms the specified format).

@atomfrede That would be nice. Besides, a plugin might be better. e.g. customFormatParser

@iamkun :heavy_check_mark: I will try to build one.

@atomfrede Cheers

I also started using this till i found out that i can't parse back to date from my format.

There are two usage scenarios for parsing in my application:

  1. Construct dayjs from a persistent storage. The strings come in the fixed ISO format. No problem.
  2. Construct dayjs from the date picker output. The strings come in a customizable user-specific format. Big problem.

The second scenario needs a custom parsing.

@atomfrede, thanks, you'll save me time to implement the customFormatParser myself :-) After the three PRs I posted, this is the last feature that I need to start using Day.js instead of Moment.js.

@atomfrede, you can have a look at #340. It is my (unfinished) implementation. Only alpha-numeric non-localizable format tokens are supported, which are usually the only ones needed in enterprise applications. Recognition of localizable tokens, like "January" is missing.

Hey guys! @iamkun thanks for so lightweight lib.
@prantlf great and so wanted feature! waiting for your pr to be approved and merged.

Sometimes, the date is user provided.

Also, is pretty strange that both '2005-02-18' and '02-18-2018' are valid dates.

I'm dying to use this library instead of momentjs! But I really need this feature to be implemented before I can switch. Bundle size & tree shaking means a lot at the moment.. ;)

If anyone is working on it, let me know if and how I can help :)

:tada: CustomParseFormat plugin is supported and included in version 1.8.0 :tada:

The release is available on:

Was this page helpful?
0 / 5 - 0 ratings