Dayjs: According to ISO 8601, the month starts with 1 through 12

Created on 11 Jun 2019  路  11Comments  路  Source: iamkun/dayjs

Hi everyone,

Please follow standards here: https://en.wikipedia.org/wiki/ISO_8601#Calendar_dates.

According to ISO 8601, the month starts with 1 through 12. Most of the libraries (i.e., NgBootstrap) follow ISO 8601 and this produces bugs while using dayjs.

Thanks!

Most helpful comment

@meness Any reason you can't just call format() or simply add 1 yourself? Seems like an over-complication with the config.

It is not really an issue right now but I like to see it later because of easy to use API point of view.

All 11 comments

We followed Javascript Date Object that starts at 0 at the moment.

We followed Javascript Date Object that starts at 0 at the moment.

You are right, the current implementation of JS Date uses 0-index for the month. Is it possible to provide a boolean for dayjs() to make it follow ISO 8601?

What do you mean "provide a boolean for dayjs() ", pls?

What do you mean "provide a boolean for dayjs() ", pls?

As there are two different implementations for the month, 0-index and 1-index, it is good to give developers a way to force Dayjs objects follow one of the implementations.

// Global options
export interface DayjsOptions {
  followIso8601: boolean // Default: false
}

Dayjs.options = {followIso8601: true}

dayjs().month() // Returns 1-index month number.

@meness Any reason you can't just call format() or simply add 1 yourself? Seems like an over-complication with the config.

@meness Any reason you can't just call format() or simply add 1 yourself? Seems like an over-complication with the config.

It is not really an issue right now but I like to see it later because of easy to use API point of view.

I produced many bugs caused by forget to month = jsMonth + 1 or jsMonth = month - 1...
IMO JavaScript specification is the "justice", but I also agree that we need some workaround for this (month - 1) problem. :joy:

Maybe something like dayjs().monthOfYear(), dayjs.isoMonth(), or dayjs().naturalMonth(), and, dayjs.iso(2019, 1, 23) or dayjs.natural(2019, 1, 23)..?

(BTW we already have Date.getFullYear() to fix original specification of Date.getYear() 馃)

Seems like an over-complication with the config.

Isn't the whole point of using a library such as this one to overcome native Date's flaws?

@ypresto's suggestion of providing an alternative API is good as it will not break BC, but it will need to be thoroughly thought through in terms of design and naming IMHO.

@neemzy Well, this is a question with no right or wrong answer. We followed the Javascript Date Object that starts at 0 and may never change.

However, we could provide a plugin or config to make it starts at 1 if there's a need. I'm more worried about it makes more confusion.

I'd go with the config option personally. Just define globally you want your months to be 1-index and never think about it any more (0-index remaining the default to preserve BC, of course). I'd say a specific API might bring more harm (in terms of confusion as you pointed out) than good.

Was this page helpful?
0 / 5 - 0 ratings