0

I am creating a simple application on Electron.

I want to save the current timestamp of an input so that I can live calculate the elapsed time of a user. What I have in mind right now is a function that saves the timestamp of the input to be deducted from the current time clock of the application to get the current elapsed time of the user.

How do I go about creating the "save current time" function? This is what I have so far:

var now = new Date(); // time now
now.getHours(); // 
now.getMinutes(); // 
now.getSeconds(); // 

for getting the current time, and:

var tableBody = '';
for (i = 0; i < students.length; i++) {
  tableBody += '<tr>';
  tableBody += '  <td style="text-transform:capitalize;">' + 
students[i].name + '</td>';
  tableBody += '  <td>' + now + '</td>';
  tableBody += '  <td><input class="btn btn-danger" type="button" 
value="Delete" onclick="deleteStudent(\'' + students[i]._id + '\')"></td>'
  tableBody += '</tr>';
}

it's propagated here in a table with the "now" variable. However, it shows up as the current time as saved on my local computer, which isn't what I want.

I apologize if this is confusing, English is not my first language and I can further clarify any questions. Thank you. I've also attached a screenshot at the bottom to help explain what I'm trying to do.enter image description here

gaborsch
  • 15,408
  • 6
  • 37
  • 48
Shaun Y
  • 41
  • 1
  • 8
  • I'd consider standardizing the timezone of your app and ui code to UTC or at least make them the same. Then you'll just need to do some conversions on the UI if you want to show the user the local time. If possible, moment.js makes this trivial. – Brian Putt Jun 29 '17 at 02:55
  • What is "*the elapsed time of a user*"? At present, you're just showing the result of *Date.prototype.toString*, which is a moment in time, not an elapsed time, and can be displayed in many, many different formats. What do you actually need? – RobG Jun 29 '17 at 03:29
  • @RobG i need the elapsed time of a user, so in essence, the current time of the locale minus the time they "clocked in" – Shaun Y Jun 29 '17 at 03:33
  • In that case, just store `var start = new Date()` when they "clock in" and the elapsed time in milliseconds is just `new Date() - start` (assuming they don't meddle with the system clock in the meantime). You might also want to read [*Where can I find documentation on formatting a date in JavaScript?*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript?s=1|21.1211) – RobG Jun 29 '17 at 04:20
  • @RobG Ok. The issue now is that var start = new Date() is a variable in which the current time/date is stored, so when i refresh the app, it changes the time to the current time/date – Shaun Y Jun 29 '17 at 04:35
  • So store it, either as a time value using *getTime* or string using *toISOString*, both of which will convert back to a date object fairly easily (except for IE 8 and ISO strings). – RobG Jun 29 '17 at 04:53

0 Answers0