{"record":{"id":"075f9b3c82977c9c","repo":"pentaho/pentaho-kettle","slug":"the-function-call-truncdate-requires-2-arguments-a-date-and","errorCode":null,"errorMessage":"The function call truncDate requires 2 arguments: a date and a level (int)","messagePattern":"The function call truncDate requires 2 arguments: a date and a level \\(int\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java","lineNumber":2433,"sourceCode":"        return null;\n      } else if ( isUndefined( ArgList[0] ) ) {\n        return undefinedValue;\n      }\n\n      // This is the truncation of a date...\n      // The second argument specifies the level: ms, s, min, hour, day, month, year\n      //\n      java.util.Date dArg1 = null;\n      Integer level = null;\n      try {\n        dArg1 = (java.util.Date) ArgList[0];\n        level = (Integer) ArgList[1];\n      } catch ( Exception e ) {\n        throw new RuntimeException( e.toString() );\n      }\n      return ScriptAddedFunctions.truncDate( dArg1, level );\n    } else {\n      throw new RuntimeException( \"The function call truncDate requires 2 arguments: a date and a level (int)\" );\n    }\n  }\n\n  @VisibleForTesting\n  static Date truncDate( Date dArg1, Integer level ) {\n    Calendar cal = Calendar.getInstance();\n    cal.setTime( dArg1 );\n    switch ( level.intValue() ) {\n      // MONTHS\n      case 5:\n        cal.set( Calendar.MONTH, 0 );\n        // DAYS\n      case 4:\n        cal.set( Calendar.DAY_OF_MONTH, 1 );\n        // HOURS\n      case 3:\n        cal.set( Calendar.HOUR_OF_DAY, 0 );\n        // MINUTES","sourceCodeStart":2415,"sourceCodeEnd":2451,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L2415-L2451","documentation":"truncDate(date, level) truncates a Date to the precision given by level (0=ms, 1=s, 2=min, 3=hour, 4=day, 5=month). The library throws this RuntimeException when the function is not called with exactly 2 arguments, since both a date and a level are mandatory to compute the truncated value.","triggerScenarios":"Calling truncDate(d) with only the date, truncDate() with none, or truncDate(d, level, extra) with a third argument in JavaScript script steps.","commonSituations":"Copy-pasting a one-argument TRUNC(date) habit from SQL/Excel where the level defaults; an optional third argument added during refactoring; a variable holding the level being undefined and dropped by argument marshalling.","solutions":["Supply exactly 2 arguments: truncDate(myDate, level)","Add the missing level argument (0..5) appropriate to the desired precision","If only the date may be missing/undefined, guard it first: if (d != null) td = truncDate(d, 2)","Use trunc() instead if you intended numeric truncation"],"exampleFix":"// before\nvar td = truncDate(orderDate);\n// after\nvar td = truncDate(orderDate, 4); // truncate to day","handlingStrategy":"validation","validationCode":"function safeTruncDate(d, level) {\n  if (arguments.length !== 2) throw new Error('truncDate requires date + level');\n  if (d == null) return null;\n  return truncDate(d, level);\n}","typeGuard":"function hasTwoArgs(fn) { return function(a, b) { if (b === undefined) throw new Error('missing level'); return fn(a, b); }; }","tryCatchPattern":"try {\n  var td = truncDate(d, 4);\n} catch (e) {\n  Logger.logError('truncDate misuse: ' + e.message);\n  var td = null;\n}","preventionTips":["Always supply both date and level arguments","Remember this truncDate has no default level, unlike some SQL dialects","If the date may be null, check it first — null date is allowed but the argument slot must exist","Add the level via a named constant (LEVEL_DAY = 4) to avoid arity slips"],"tags":["javascript","scripting","argument-count","date"],"backgroundTag":"missing-required-argument","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}