标签云

微信群

扫码加入我们

WeChat QR Code

I have an annoying bug in on a webpage:date.GetMonth() is not a functionSo, I suppose that I am doing something wrong. The variable date is not an object of type Date. How can I check for a datatype in Javascript? I tried to add a if (date), but it doesn't work.function getFormatedDate(date) {if (date) { var month = date.GetMonth();}}So, if I want to write defensive code and prevent the date (which is not one) to be formatted, how do I do that?Thanks!UPDATE: I don't want to check the format of the date, but I want to be sure that the parameter passed to the method getFormatedDate() is of type Date.


Out of interest do you know the reason for this failing when passing across frame boundaries?

2019年07月20日27分23秒

Simon: JS globals are local to the current global object (aka window or self); different frames have their own global objects, and their properties (ie globals) refer to distinct objects: Date in frame1 is a different function object than Date in frame2; the same is true for Date.prototype, which is the reason for the instanceof failure: Date.prototype from frame1 is not part of the prototype chain of Date instances from frame2

2019年07月20日27分23秒

Christoph, what do you call "frame"? IFRAME, each frame in FRAMESET or something else (I mean JS-specific, not the HTML-thing)?

2019年07月20日27分23秒

new Date('something') instanceof Date returns true in Chrome. That won't work then.

2019年07月20日27分23秒

Detecting a Date type object (as opposed to a plain Object or a string) and validating an object you expect to be a Date are two different tasks.There are a number of situations where the input to your function could be one of a number of different data types. In my case, I can trust that any Date object I get is valid (it's not coming straight from a client)If validating is a concern, here is a post with a number of options. stackoverflow.com/questions/1353684/…

2019年07月20日27分23秒

Why is this not the accepted or more upvoted answer? Simply checking if date has a .getMonth property could trigger a false positive.

2019年07月20日27分23秒

instanceof can trigger false negatives, see Christoph's comment to his own answer.

2019年07月21日27分23秒

doremi Here is a demo of instanceof triggering false negative: jsbin.com/vufufoq/edit?html,js,console

2019年07月20日27分23秒

That should be marked as answer. Perfect.

2019年07月21日27分23秒

One gotach, be sure and use the global isNaN() and not Number.isNaN() since it will validate an Invalid Date

2019年07月20日27分23秒

Check whether it's callable: if (date.getMonth && typeof date.getMonth === "function") {...}

2019年07月20日27分23秒

This worked for me, thanks. However, if you pass a single digit such as 0, or 1, it treats that as a valid Date... any thoughts?

2019年07月21日27分23秒

That's right, RicardoSanchez.You probably want to use the accepted answer (Object.prototype.toString.call(value) === '[object Date]') if it's possible you'll be getting numbers.The method in this answer really tells you whether the value is convertible to a Date.

2019年07月21日27分23秒

This doesn't work. Your true line actually is false and the question is about checking if an object is a date object...

2019年07月21日27分23秒

OK. So yo can try: if(object instanceof Date) ...

2019年07月20日27分23秒

jspassov answer is more accurate with if a string is a date or not. That I was looking for. Thanks!!

2019年07月20日27分23秒

This is the best answer for simply checking whether a string is a date or not

2019年07月21日27分23秒

isDate(new (function AnythingButNotDate(){ })()) returns true

2019年07月20日27分23秒

Any custom made object with the constructor name "Date" returns "Date" too which is as risky as just checking if the parameter has getMonth property.

2019年07月20日27分23秒

boghyon sounds like whomever creates an object with the constructor name of a already predefined Javascript standard library is not following best practices in the first place. That would be like downloading lodash then creating your own lodash module and expecting things to work.

2019年07月20日27分23秒

Christoph's answer is more accurate. Having a 'call' property doesn't necessarily mean it is a function!

2019年07月20日27分23秒

Nice and short! But unfortunately, it has the same issue as instanceof.

2019年07月20日27分23秒

BoghyonHoffmann in case of iFrame it may look like:iWindow.Date.prototype.isPrototypeOf(iWindow.date); // trueiWindow.date instanceof iWindow.Date; // true

2019年07月20日27分23秒

No, only the date object has the isValid method

2019年07月20日27分23秒

grumpy nikkwong No and no. The standard date object doesn't have isValid. Only moment.js has such an API.

2019年07月20日27分23秒