dayjs().isValid() how to use

Created on 17 Aug 2018  ·  6Comments  ·  Source: iamkun/dayjs

dayjs() 返回当前系统时间
dayjs().isValid(); 肯定为true
那么传入一个参数呢?
dayjs(‘2018-08-107’).isValid();
dayjs(‘2018-13-01’).isValid();
dayjs(‘1-10-01’).isValid();
结果都是true!

是我打开方式不对吧 “...“

Most helpful comment

Actually,
new Date(2018,13,01) => 2019-01-31T16:00:00.000Z
new Date('2018-08-107') => Invalid Date
new Date('1-10-01') => Invalid Date

But
dayjs(‘2018-13-01’).isValid(); => true
dayjs(‘2018-08-107’).isValid(); => true
dayjs(‘1-10-01’).isValid(); => true

so, there must be something wrong with the isValid function.
isValid() { return !(this.$d.toString() === 'Invalid Date') }

All 6 comments

It's a bit weird... If one inputs 2018-13-01 the dayjs object will be 2019-01-01...

@rfoel

Javascript did the same.

new Date(2018, 13 , 01) => valid date

@rfoel:
Because in its inner code, it just calls something like new Data():

Example:new Date(2018,13,01) => 2019-01-01

isValid is just from this:https://github.com/iamkun/dayjs/blob/002b69520415e0005117db51c9ef5aecc0ed8741/src/index.js#L91

Actually,
new Date(2018,13,01) => 2019-01-31T16:00:00.000Z
new Date('2018-08-107') => Invalid Date
new Date('1-10-01') => Invalid Date

But
dayjs(‘2018-13-01’).isValid(); => true
dayjs(‘2018-08-107’).isValid(); => true
dayjs(‘1-10-01’).isValid(); => true

so, there must be something wrong with the isValid function.
isValid() { return !(this.$d.toString() === 'Invalid Date') }

@kikaendeavor

new Date('1-10-01') => Wed Jan 10 2001 00:00:00

@xxyuk

woooh~~~~
the result is different between chrome and firefox.
firefox doesn't support string like '1-10-01', but new Date(1,10,01) is ok.

format like (yyyy, mm-1, dd, hh, mm, ss) or (mm/dd/yyyy hh:mm:ss) are much more compatible.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

paraself picture paraself  ·  3Comments

fundon picture fundon  ·  5Comments

lyz810 picture lyz810  ·  4Comments

Sunrise1970 picture Sunrise1970  ·  4Comments

g1eny0ung picture g1eny0ung  ·  4Comments