Skip to main content
Participating Frequently
September 12, 2018
Answered

Converting time with scand is giving me NULL

  • September 12, 2018
  • 2 replies
  • 596 views

I'm trying to get the processing start time and then subtract it from the processing end time. I need to convert the time before doing the math, right?

both of my scand commands give me NULL. Can you guys see what I did wrong here?

var startTime = util.printd("HH:MM:ss", new Date());
var compStartTime = util.scand("HH:MM:ss",startTime);
app.alert("start time converted: "+compStartTime);

// Do some stuff

var finishTime = util.printd("HH:MM:ss", new Date());
var compFinishTime = util.scand("HH:MM:ss", finishTime);

var calcTime = compFinishTime - compStartTime;
var finalTime = util.printd("HH:MM:ss",calcTime);
app.alert(finalTime);

Thanks for your help!

Rick

This topic has been closed for replies.
Correct answer BarlaeDC

Hi,

I think it is a little complicated, as I don't think you need to worry about format until you are ready to show it to the user so you code could be simplified to

function msToTime(duration) {

  var milliseconds = parseInt((duration % 1000) / 100),

    seconds = parseInt((duration / 1000) % 60),

    minutes = parseInt((duration / (1000 * 60)) % 60),

    hours = parseInt((duration / (1000 * 60 * 60)) % 24);

  hours = (hours < 10) ? "0" + hours : hours;

  minutes = (minutes < 10) ? "0" + minutes : minutes;

  seconds = (seconds < 10) ? "0" + seconds : seconds;

  return hours + ":" + minutes + ":" + seconds + "." + milliseconds;

}

var startTime = new Date();

// Do some stuff

var finishTime = new Date();

var finalTime = finishTime - startTime;

app.alert ( msToTime ( finalTime));

Hope this helps, and there a probably other ways.

Malcolm

2 replies

BarlaeDC
Community Expert
BarlaeDCCommunity ExpertCorrect answer
Community Expert
September 12, 2018

Hi,

I think it is a little complicated, as I don't think you need to worry about format until you are ready to show it to the user so you code could be simplified to

function msToTime(duration) {

  var milliseconds = parseInt((duration % 1000) / 100),

    seconds = parseInt((duration / 1000) % 60),

    minutes = parseInt((duration / (1000 * 60)) % 60),

    hours = parseInt((duration / (1000 * 60 * 60)) % 24);

  hours = (hours < 10) ? "0" + hours : hours;

  minutes = (minutes < 10) ? "0" + minutes : minutes;

  seconds = (seconds < 10) ? "0" + seconds : seconds;

  return hours + ":" + minutes + ":" + seconds + "." + milliseconds;

}

var startTime = new Date();

// Do some stuff

var finishTime = new Date();

var finalTime = finishTime - startTime;

app.alert ( msToTime ( finalTime));

Hope this helps, and there a probably other ways.

Malcolm

Participating Frequently
September 12, 2018

Thanks Malcolm and George!

Inspiring
September 12, 2018

rickr43378632  wrote

I need to convert the time before doing the math, right?

No. Consider the following

var t1 = new Date();

// Do stuff

var t2 = new Date();

// Show user the number of milliseconds between t2 and t1

app.alert(t2 - t1);