Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  
</body>
</html>
 
function myFunction(date) {
  var args = date.split(/[^0-9]+/),
      i, l = args.length;
  
  // Prepare args
  for(i=0;i<l;i++) {
    if(!args[i]) {
      args.splice(i--,1);
      l--;
    } else {
      args[i] = parseInt(args[i], 10);
    }
  }
  
  // Check month
  if(args[1] < 1 || args[1] > 12) {
    throw new Error('Invalid month');
  }
  // Check day
  if(args[0] > new Date(args[2], args[1], 0).getDate()) {
    throw new Error('Invalid date');
  }
  
  return new Date(args[2], args[1]-1, args[0]);
  
}
console.log( myFunction('-23 - 11-+~/2013').toString() );
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers