Community Tip - You can change your system assigned username to something more personal in your community settings. 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;
I found that using the row below did not work.
searchTable.Find({timestamp: currentTimestamp});
So instead I used an if statement to find the correct row using the timestamp field: if(searchTable.timestamp - currentTimestamp == 0){
// to calculate duration, instead of using the dateDifference snippet, I used:
let duration =
currentTimestamp - otherTime;
}
This worked well and my timestamp column gets updated by the duration values.