1

When inputing string with number the new Date() is missbehaving, is there another way how to achieve this without regex?

let string = 'some string 1'; let invalid = new Date(string); 

it is still parsing as a Date value

Mon Jan 01 2001 00:00:00 GMT+0000 (Greenwich Mean Time) 

I'm expecting it as an invalid date

Update

let string = 'some string 1'; let invalid = new Date(string.replace(/\s/g, '')); 

Seem to resolve to Invalid Date as i want to and it seem to proper validate all dates also human readable dates like Sun, 1 Dec 2024 is handled correctly returning that it is a date indeed.

but still it have missbehaviour when entering

let string = 'some string, 0'; let invalid = new Date(string.replace(/\s/g, '')); console.log(invalid) Sat Jan 01 2000 00:00:00 GMT+0000 (Greenwich Mean Time) 
6
  • 2
    FWIW this is implementation specific. Internally this will be using Date.parse which only technically supports one format - anything else is up to the implementation to decide how to handle. In chrome at least, it appears to do as you've described. In Firefox, it returns an invalid date. Commented Jun 17, 2024 at 10:53
  • On possible solution: Check if date is 01/01/2001 00:00:00 then claim it's not valid. This may not be viable, depending on what you need the date for. If it's "enter a date in the last 20 years" then it's ok to check for this. Commented Jun 17, 2024 at 11:10
  • @JamesThorpe - in my Firefox it returns a Jan 1 2001 date Commented Jun 17, 2024 at 11:10
  • IMHO, attempting to accept everything but blatantly invalid input is a no-go, no matter how terribly popular the approach is. The mere existence of ambiguous formats (is 10/12/2024 American October or European December?) should discourage so. Require users to provide a fixed specific format and reject everything that doesn't match. Commented Jun 17, 2024 at 11:16
  • 1
    this is one time libraries can help - don't re-invent the wheel - research javascript date libraries and find one that is suitable to your requirements Commented Jun 17, 2024 at 11:37

2 Answers 2

0

If you don't mind using javascript library and the date string input is in fixed format, try Moment.js

var string = 'some string 1'; var validDate = moment(string, 'ddd, D MMM YYYY'); console.log(validDate.isValid()); // return false var string = 'Sun, 1 Dec 2024'; var validDate = moment(string, 'ddd, D MMM YYYY'); console.log(validDate.isValid()); // return true 
Sign up to request clarification or add additional context in comments.

2 Comments

So the moment library is i believe deprecated that is why i do not want to use it
@Dzi Then, you may need to learn Luxon. It has similar function with momentjs. But, I don't use it yet so I can't give you code example.
-1

Depending on the Data that gets passed, it could be quite simple to check if date is valid. You can use isNaN for this.

let string = 'some string 1'; let invalid = !isNaN(new Date(string)); if (invalid) { console.log("Not a Valid string"); } else { console.log("Date is valid"); }

This will return true if the string is "kind of" valid. e.g abc 1. In this case the date function assumes that this could be a day or so. Than he would expect 1 is the month. abc 13 would end up in not valid because there is no month 13. So you should consider what data you really get, not what theoreticle possible.

You could build larger validations or might go for some online libs

similar to moment.js (deprecated).

12 Comments

i had tis check there and it is still return (date !== 'Invalid Date') && !isNaN(date); but maybe the problem is and?
Actually new Date('foobar') returns Invalid Date as string. Obvirously you have a problem with your check there, yes. You dont need to check both.
new Date('foobar 1'); was returning a date for me it is checking for both but i double cheked that is not causing the issue, try to run you snippet with new Date('foobar , 1') it was not about the string but string with number also i need human readable dates
It actually thinks that 1 is jan, you can switch the number up to 12, after that it is invalid again. What are the actuall contents that you may could pass? i mean in what case could it be an invalid string that gets passed into the date function ?
you may could go with moment.js and use the isValidmethod. Perhaps...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.