-1

I have a program, that generates a repport in a CSV file. Problem is, that the timestamps is in Unix Timestamp format. So i need to access the specific time stamp element in a forEach loop and modify that. I'm quite new to JavaScript. If it was Python, it would have been no problem..

The code looks like this:

for (let electronicRecord of result.Values) {
      fields.forEach((f, i) => csvData += electronicRecord[f] + (i == fields.length -1 ? "\n" : delimiter));
    }

I know that the value i want to change is "ElectronicRecord["TimeStamp"], but how do i access that in a forEach loop?

for (let i in fields) {
        if (i == "TimeStamp") {
          csvData += electronicRecord[i]
        } else if (fields.indexOf(i) == fields.length - 1) {
        csvData += electronicRecord[i] + "\n";
      } else {
        csvData += electronicRecord[i] + delimiter;
      }
    }

This was my idea, but it doesn't seem to be working. fields is an array with the "headlines" for the CSV file.

The original function in my system is:

// Please note, the format of a file path is device and operating system specific 
// You need to adapt the given path to your environment 
// Example for a Unified Comfort  device (Linux OS): /home/user1/data.dat 
// Example for a Unified PC-based device (MS Windows OS): C:\\Users\\Public\\data.dat 

const startDate = "2020-09-01 08:00:00.000";
const endDate = "2020-10-01 08:00:00.000";

let moreFollows = false;
let page = 0;

let fields = ["TimeStamp", "AuditProviderType", "AuditProvider", "ObjectReference","ObjectName", "OperationType", "OperatorStation", "User", "OldValue", "NewValue", "Reason", "Language", "Signature", "Integrity"];
let delimiter = ",";
let csvData = "";
fields.forEach((f, i) => csvData += f + (i == fields.length - 1 ? "\n" : delimiter));

do {
  try {
    const result = await HMIRuntime.Audit.SysFct.ReadElectronicRecord(startDate, endDate, page);
    moreFollows = result.More;
    for (let electronicRecord of result.Values) {
      fields.forEach((f, i) => csvData += electronicRecord[f] + (i == fields.length -1 ? "\n" : delimiter));
    }
    page++;
  } catch(ex) {
    HMIRuntime.Trace("Error reading electronic record. Error: " + ex.message);
    return;
  }
} while (moreFollows);

try {
  let fileName = "C:\\Users\\Public\\AuditFile.csv";
  await HMIRuntime.FileSystem.WriteFile(fileName, csvData, "utf8");
  HMIRuntime.Trace("Write file finished successfully");
} catch(ex) {
  HMIRuntime.Trace("Error writing file. Error: " + ex.message);
}
  • `electronicRecord[i].TimeStamp` ??? – Chris G May 03 '23 at 12:00
  • So are you generating the CSV file or reading it? Or both? – MaBed May 03 '23 at 12:01
  • I am generating it by reading some data from the system. And i would like to have a date and time in the "TimeStamp" column, so i need to modify the timestamp value from the system before i write it to the CSV file. – numberonegunner May 03 '23 at 12:03
  • And `electronicRecord` is the data you get from system, right? What format do they have? It is some Object (and if so, what are the properties) or Array? – MaBed May 03 '23 at 12:06
  • create a [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) we wasting time guessing how to access your data if we don't know how your data looks like – Chris G May 03 '23 at 12:06
  • ElectronicRecord is a object with 14 properties. Is there a way when using the forEach loop to manipulate the data written to the csvData string? – numberonegunner May 03 '23 at 12:10
  • if it is a single object instead of an array then `electronicRecord.TimeStamp` should be enough to access it, no need for a loop. You only need a loop if you trying to access an array – Chris G May 03 '23 at 12:12
  • Each time the for loop is executed it is with a "electronicRecord" object with 14 properties, one of them is "TimeStamp". I would like to change that specific data, but not the rest in the forEach loop. – numberonegunner May 03 '23 at 12:12
  • Does this answer your question? [Function to convert timestamp to human date in javascript](https://stackoverflow.com/questions/19485353/function-to-convert-timestamp-to-human-date-in-javascript) – tevemadar May 03 '23 at 15:07

1 Answers1

0

To acess the "TimeStamp" value of each "electronicRecord" object in the forEach loop, you can modify the loop to use the for...of syntax and then access the "TimeStamp" property:

for (let electronicRecord of result.Values) {
  for (let i in fields) {
    if (fields[i] === "TimeStamp") {
      // Convert Unix Timestamp to JavaScript timestamp (milliseconds)
      let jsTimestamp = electronicRecord[fields[i]] * 1000;
      // Format the timestamp as desired
      let formattedTimestamp = new Date(jsTimestamp).toLocaleString();
      csvData += formattedTimestamp;
    } else if (fields.indexOf(fields[i]) === fields.length - 1) {
      csvData += electronicRecord[fields[i]] + "\n";
    } else {
      csvData += electronicRecord[fields[i]] + delimiter;
    }
  }
}
Klone
  • 290
  • 1
  • 2