Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
Good day all,
I have a table that contains a timestamp field.
I want to replace the values in this field with a specific dataDifference calculation that I make.
The two dates that I use to find the difference are inputs.
I have written the code below. It seems like the issue occurs when I assign the new timestamp value to the table.
Has anyone tried doing this before?
Your response is much appreciated.
You posted your code, but can you describe what "the issue" is? Do you get an error?
Also, when you have two timestamps, and you compute the difference, note that the result is not a timestamp, but a number.
// dateDifference(date1:DATETIME, date2:DATETIME):NUMBER
let difference = dateDifference(date1, date2);
So you probably can't assign that to a timestamp property,
I do not get an error but my timestamp column does not update.
Well, as I said, you can't assign a NUMBER value to a TIMESTAMP column.
Your code doesn't look too bad, but you can put in loggings to see what is happening. Not sure what is cloning is for. Try this:
// setting up some sample data
let currentTimestamp=new Date();
let otherTime=dateAddMonths(new Date(),-2);
var searchTable = Resources["InfoTableFunctions"].CreateInfoTable();
searchTable.AddField({name: 'token', baseType: 'STRING'});
searchTable.AddField({name: 'duration', baseType: 'NUMBER'});
searchTable.AddField({name: 'timestamp', baseType: 'DATETIME'});
searchTable.AddRow({ token:"test",duration:0, "timestamp":currentTimestamp});
// your version would be:
//searchTable = testMode?Things["ThingName"].ChartData[0].Data:data;
let searchForTimestampRow = searchTable.Find({timestamp: currentTimestamp});
if (searchForTimestampRow) {
logger.warn("Found Row with timestamp "+currentTimestamp);
let duration = dateDifference(currentTimestamp, otherTime);
logger.warn("duration="+duration);
searchForTimestampRow.timestamp = dateAddYears(new Date(),1); // just to show it's updating
searchForTimestampRow.duration = duration; // duration is a number
}
result=searchTable;