网上搜到的都是类似如下这种:
// 获取当前时间
var date= new Date();// 格式化输出当前时间
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();var formattedDate = year + "-" + ( month + 1 ) + "-" + day + " " + hour + ":" + minute + ":" + second ;console.log(formattedDate); // 输出:yyyy-MM-dd HH:mm:ss
将其写入到 jetlinks 规则编排函数节点中:
var ctx = context;handler.onMessage(function(ruleData){var reportTime = getReportTime();ctx.getLogger().warn("当前系统时间:");ctx.getLogger().warn(reportTime );return ruleData.data
})function getReportTime(){// 获取当前时间var date = new Date();// 格式化输出当前时间var year = date.getFullYear();var month = date.getMonth();var day = date.getDate();var hour = date.getHours();var minute = date.getMinutes();var second = date.getSeconds();var reportTime = year + "-" + ( month + 1 ) + "-" + day + " " + hour + ":" + minute + ":" + second ;return reportTime;}
运行报错:
java.lang.UnsupportedOperationException: can not cast to number:-at org.jetlinks.reactor.ql.utils.CastUtils.castNumber(CastUtils.java:160)Suppressed: The stacktrace has been enhanced by Reactor, refer to additional information below:
Assembly trace from producer [reactor.core.publisher.FluxDefer] :reactor.core.publisher.Flux.deferorg.jetlinks.pro.rule.engine.executor.ScriptTaskExecutorProvider.lambda$createExecutor$8(ScriptTaskExecutorProvider.java:77)
Error has been observed at the following site(s):*_______Flux.defer ⇢ at org.jetlinks.pro.rule.engine.executor.ScriptTaskExecutorProvider.lambda$createExecutor$8(ScriptTaskExecutorProvider.java:77)|_ Flux.mapNotNull ⇢ at org.jetlinks.pro.rule.engine.executor.ScriptTaskExecutorProvider.lambda$createExecutor$8(ScriptTaskExecutorProvider.java:95)...
输出如下几个变量的类型:
var ctx = context;handler.onMessage(function(ruleData){var reportTime = getReportTime();ctx.getLogger().warn("当前系统时间:");ctx.getLogger().warn(reportTime );return ruleData.data
})function getReportTime(){// 获取当前时间var date = new Date();// 格式化输出当前时间var year = date.getFullYear();var month = date.getMonth();var day = date.getDate();var hour = date.getHours();var minute = date.getMinutes();var second = date.getSeconds();ctx.getLogger().warn( typeof year );ctx.getLogger().warn( typeof month );ctx.getLogger().warn( typeof day );ctx.getLogger().warn( typeof hour );ctx.getLogger().warn( typeof minute );ctx.getLogger().warn( typeof second );var reportTime = year + "-" + ( month + 1 ) + "-" + day + " " + hour + ":" + minute + ":" + second;return reportTime;}
发现都是 number 类型:
怀疑是这里的语法要比 浏览器中的 javascript 语法要严格一些,数字类型不能和字符串类型直接相加,需要先转成字符串,修改后的脚本如下:
var ctx = context;handler.onMessage(function(ruleData){var reportTime = getReportTime();ctx.getLogger().warn("当前系统时间:");ctx.getLogger().warn(reportTime );return ruleData.data
})function getReportTime(){var date = new Date();var year = date.getFullYear().toString();var month = date.getMonth() + 1;if (month < 10) {month = '0' + month.toString();} else {month = month.toString();}var day = date.getDate();if (day < 10) {day = '0' + day.toString();} else {day = day.toString();}var hours = date.getHours();if (hours < 10) {hours = '0' + hours.toString();} else {hours = hours.toString();}var minutes = date.getMinutes();if (minutes < 10) {minutes = '0' + minutes.toString();} else {minutes = minutes.toString();}var seconds = date.getSeconds();if (seconds < 10) {seconds = '0' + seconds.toString();} else {seconds = seconds.toString();}var reportTime = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':00';return reportTime;}
运行发现果然成功了^_^。