dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

Bad judgment :isValid

Open KilianShen opened this issue 1 year ago • 4 comments

Describe the bug A clear and concise description of what the bug is.

Expected behavior A clear and concise description of what you expected to happen.

Information

  • Day.js Version [e.g. v1.11.13]
  • OS: [e.g. win]
  • Browser [e.g. chrome 113]
  • Time zone: [e.g. GMT-07:00 DST (Pacific Daylight Time)] image

KilianShen avatar Dec 05 '24 07:12 KilianShen

I don't think this is a bug, because when we start a Date Object with a number, it is read as if it were milliseconds. For example: JavaScript is based on Epoch Time, so new Date(0) is like 1970-01-01 00:00:00 and new Date(1000) is like 1970-01-01 00:00:01.

ryanvieira-gbmtech avatar Dec 11 '24 21:12 ryanvieira-gbmtech

image

pccai avatar Dec 22 '24 14:12 pccai

String.prototype.isValid = function() { return this.length > 0; //a demo };

pccai avatar Dec 22 '24 14:12 pccai

I don't think this is a bug, because when we start a Date Object with a number, it is read as if it were milliseconds. For example: JavaScript is based on Epoch Time, so new Date(0) is like 1970-01-01 00:00:00 and new Date(1000) is like 1970-01-01 00:00:01.

I agree, it is not a bug, it is just poorly designed.

dayjs(v, 'YYYY-MM-DDTHH:mm:ssZ', true).isValid() -> true makes the isValid() fn useless, even in strict mode

I would recommend not using .isValid() at all and use regex instead.

lveillard avatar Mar 03 '25 13:03 lveillard