Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for leapyear.js

(view source code of leapyear.js as plain text)

  1. // LeapYear.js,  Version 1.01 for Windows Script Host
  2. // Check if the specified year is a leap year.
  3. //
  4. // Written by Rob van der Woude
  5. // http://www.robvanderwoude.com
  6.  
  7. // Parse command line
  8. objArgs = WScript.Arguments;
  9. if ( objArgs.length == 0 ) {
  10. 	// Default is current year
  11. 	objToday = new Date();
  12. 	myYear = objToday.getFullYear();
  13. };
  14. if ( objArgs.length == 1 ) {
  15. 	// Check datatype and range of argument
  16. 	myYear = objArgs(0).valueOf( );
  17. 	if ( isNaN( myYear ) ) Syntax( );
  18. 	if ( myYear <    0 )   Syntax( );
  19. 	if ( myYear > 9999 )   Syntax( );
  20. };
  21. if ( objArgs.length > 1 ) Syntax( );
  22.  
  23. isLeapYear = 0;
  24. if ( myYear %   4 == 0 ) isLeapYear = 1;
  25. if ( myYear % 100 == 0 ) isLeapYear = 0;
  26. if ( myYear % 400 == 0 ) isLeapYear = 1;
  27.  
  28. if ( isLeapYear == 1 ) {
  29. 	strIs = " IS";
  30. } else {
  31. 	strIs = " is NOT";
  32. };
  33. WScript.Echo( myYear + strIs + " a leap year" );
  34.  
  35. // Done
  36. WScript.Quit( isLeapYear );
  37.  
  38.  
  39. function Syntax( ) {
  40. WScript.Echo( );
  41. WScript.Echo( "LeapYear.js,  Version 1.01 for WSH" );
  42. WScript.Echo( "Check if the specified year is a leap year." );
  43. WScript.Echo( );
  44. WScript.Echo( "Usage:   CScript  LEAPYEAR.JS  [ year ]" );
  45. WScript.Echo( );
  46. WScript.Echo( "Where:   'year' should be within the range of 0 through 9999." );
  47. WScript.Echo( "         Default is the current year, if none is specified." );
  48. WScript.Echo( );
  49. WScript.Echo( "Returns: 0  if NOT a leap year" );
  50. WScript.Echo( "         1  on leap years" );
  51. WScript.Echo( "         2  on syntax errors" );
  52. WScript.Echo( );
  53. WScript.Echo( "Written by Rob van der Woude" );
  54. WScript.Echo( "http://www.robvanderwoude.com" );
  55. WScript.Quit(2);
  56. }
  57.  

page last modified: 2024-04-16; loaded in 0.0052 seconds