javascript - subtract time between two values set in localstorage -


i have quiz program written in vanilla js. supposed log time takes complete it.

what achieve how long takes user answer questions subtraction 2 variables (strings). if possible.

when user has stated name , presses button "start quiz" currrent time logged in localstorage so:

var storename; var d = new date(); var h = gethours(); var m = getminutes(); var s = getseconds(); var starttime = h + ":" + m + ":" + s; var endtime = h + ":" + m + ":" + s; var result;  var storage = {     storename: storename,     starttime: starttime,     endtime: null,     result: result }; 

the tricky part not know how subtract starttime endtime time took answer questions. quiz on in minutes, use hour redundant.

when user has clicked "submit" answer on last question want time logged in ls endtime.

i hope have not been unclear , thank in advance time. thank you.

instead of storing string date, directly store time in milliseconds. can subtract start time end time figure out time difference.

var starttime = date.now(); var storage = {     storename: storename,     starttime: starttime,     endtime: null,     result: result }; 

later can calculate endtime using date.now() , subtract starttime time difference.

storage.endtime = date.now();  //difference var diff = storage.endtime - storage.starttime; 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -