2

I'm trying to write a java code into javascript.

public static String generateTimeRange( start,  end) {
    return String.format("%02d:%02d-%02d:%02d", start / 60, start % 60, end / 60, end % 60);
}

Basically this will return a time 00:10 - 00:20 (if 10 and 20 are arguments)

I want to write the same function in javascript but having a hard time. Can someone help?

function generateTimeRange( start,  end) {
    return String.format("{0}:{1}-{2}:{3}", start / 60, start % 60, end / 60, end % 60);
}

Since javascript does not have String.format, I've used the following code:

if (!String.format) {
  String.format = function(format) {
    var args = Array.prototype.slice.call(arguments, 1);
    return format.replace(/{(\d+)}/g, function(match, number) { 
      return typeof args[number] != 'undefined'
        ? args[number] 
        : match
      ;
    });
  };
}

But it kinda prints the following

generateTimeRange(10,20);
"0.16666666666666666:10-0.3333333333333333:20"

Looks like I'm not doing the %02d. How do i do it in javascript?

Thanks

1

4 Answers 4

1

You could just return a concatenate string or you could do it with Template literals

function generateTimeRange(start, end) {
  return Math.floor(start / 60) + ":" + Math.floor(start % 60) + "-" + Math.floor(end / 60) + ":" + Math.floor(end % 60);
}

function generateTimeRangeInterpolation(start, end) {
  var sPrefix = Math.floor(start / 60),
    start = Math.floor(start % 60),
    ePrefix = Math.floor(end / 60),
    end = Math.floor(end % 60);

  return `${sPrefix}:${start}-${ePrefix}:${end}`
}

console.log(generateTimeRange(10, 20));
console.log(generateTimeRangeInterpolation(10, 20));

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your response.
1

javascript es6 introduces string template that can solve your problem.

function _pad(_n, width) {
  const z = '0';
  const n = _n + '';
  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n
}

function formatTime(start, end) {
  const minStart = _pad(parseInt(start / 60), 2)
  const secStart = _pad(start % 60, 2)
  const minEnd = _pad(parseInt(end / 60), 2)
  const secEnd = _pad(end % 60, 2)
  
  return `${minStart}:${secStart}-${minEnd}:${secEnd}`
}

console.log(formatTime(100, 120)) // 01:40-02:00
console.log(formatTime(60, 61)) // 01:00-01:01

Comments

1

Padded zeros, correct? I use this one from this post

SNIPPET

String.prototype.zer0Pad = function(pad, siz) {
  var str = this;
  var len = parseInt(siz, 10);
  while (str.length < len) {
    str = pad + str;
  }
  return str;
}

var number = '7';
var digits = '2';
var padding = '0';

var result1 = number.zer0Pad(padding, digits);

var string = '42';

var result2 = string.zer0Pad(0, 4);

console.log('result1: ' + result1 + '\nresult2: ' + result2);

Comments

0

i guess the quesions changes to how to make a fixed length num. you can use substr like :

function(num) { ( "00" + parseInt(num)).substr(-2); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.