0

Is there a way to determine which method is used when doing JavaScript coding. I'm playing around with datejs library and method Date.parse and Javascript Date.parse

How can I know which one is doing its job?

http://jsfiddle.net/wilfff/DL6QZ/2/

var d = "Wed Apr 29 08:53:31 +0000 2009";
function parse_date(date_str) {
    return Date.parse(date_str.replace(/^([a-z]{3})( [a-z]{3} \d\d?)(.*)( \d{4})$/i, '$1,$2$4$3'));
}
$(".foo1").text(parse_date(d));

(remove the External Resources and run again) to view the difference.

Any idea how to handle this?

Cheers!

Wilff
  • 23
  • 1
  • 7

2 Answers2

1

You can check it like this:

if(Date.parse.toString() == 'function parse() { [native code] }'){
    // native method
}

See this

Community
  • 1
  • 1
karaxuna
  • 26,752
  • 13
  • 82
  • 117
  • I wouldn't trust the white space in the above to remain consistent for all browsers (I'm looking at you, IE). At least strip them before comparing: Date.parse.toString().replace(/[ \t\r\n]/g, '') == 'functionparse(){[nativecode]}' – VeeTheSecond Aug 12 '13 at 13:23
  • @VeeTheSecond you are right, my code is tested only in chrome. I only showed the way how it can be done. For more complex code I have provided link to similar question – karaxuna Aug 12 '13 at 13:29
1

For something like this I would do my own check, and I would check for something I know would only exist, if the library is loaded. In this case you can check this:

if(Date.CultureInfo){
   // i know now I'm using the external date library
}else{
   // using the browsers native code
}
ericjbasti
  • 2,085
  • 17
  • 19