{"record":{"id":"8a3543dfad2f8869","repo":"pentaho/pentaho-kettle","slug":"please-provide-a-valid-date-to-the-function-call-date2str","errorCode":null,"errorMessage":"Please provide a valid date to the function call date2str.","messagePattern":"Please provide a valid date to the function call date2str\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java","lineNumber":1092,"sourceCode":"            throw new RuntimeException( \"Locale \" + sArg3 + \" is not 2 characters long.\" );\n          }\n        } catch ( Exception e ) {\n          throw new RuntimeException( \"Could not apply the local format for locale \"\n            + sArg3 + \" with the given format \" + sArg2 + \" on the string for \" + sArg1 + \" : \" + e.getMessage() );\n        }\n        break;\n      default:\n        throw new RuntimeException( \"The function call str2date requires 1, 2, 3, or 4 arguments.\" );\n    }\n    return oRC;\n  }\n\n  public static Object date2str( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,\n    Object FunctionContext ) {\n    Object oRC = new Object();\n    switch ( ArgList.length ) {\n      case 0:\n        throw new RuntimeException( \"Please provide a valid date to the function call date2str.\" );\n      case 1:\n        try {\n          if ( isNull( ArgList ) ) {\n            return null;\n          } else if ( isUndefined( ArgList ) ) {\n            return undefinedValue;\n          }\n          java.util.Date dArg1 = (java.util.Date) ArgList[0];\n          if ( dArg1.equals( null ) ) {\n            return null;\n          }\n          Format dfFormatter = new SimpleDateFormat();\n          oRC = dfFormatter.format( dArg1 );\n        } catch ( Exception e ) {\n          throw new RuntimeException( \"Could not convert to local format.\" );\n        }\n        break;\n      case 2:","sourceCodeStart":1074,"sourceCodeEnd":1110,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L1074-L1110","documentation":"date2str converts a java.util.Date to a string, and the first (only required) argument must be a date. Calling it with zero arguments hits case 0 of the switch and throws this RuntimeException telling you a valid date is required.","triggerScenarios":"Invoking date2str() with no arguments in a JavaScript script step; the switch's case 0 unconditionally throws before any null-handling runs.","commonSituations":"Copy/paste mistakes leaving off the date argument; a variable that was expected to hold the date being undefined so the call collapses to date2str(); misunderstanding that the date is mandatory while format/locale/timezone are optional.","solutions":["Pass a java.util.Date as the first argument, e.g. date2str(new Date()).","Verify the variable holding the date is actually populated at call time.","If the date may be absent, guard before calling: if (dateVar != null) date2str(dateVar).","Remember format/locale/timezone are optional (args 2-4); only the date is required."],"exampleFix":"// before\nvar s = date2str();\n// after\nvar s = date2str(new Date());","handlingStrategy":"validation","validationCode":"if (dateVar == null) throw new Error('date2str requires a date argument');\nvar s = date2str(dateVar);","typeGuard":"function isDateLike(v) { return v != null && v.getClass && /Date/.test(String(v.getClass())); }","tryCatchPattern":"try { var s = date2str(d); } catch (e) { if (String(e.message).indexOf('Please provide a valid date') >= 0) { throw new Error('date argument missing/undefined at call site'); } throw e; }","preventionTips":["Always pass the date as the first argument","Check the date variable is populated before calling (null check)","Convert strings/timestamps to Date via str2date() first","Remember only the date is required; format/locale/timezone are optional"],"tags":["javascript","argument-count","date-formatting","scripting"],"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"}