• If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Announcement

Collapse
No announcement yet.

formating Time stored as a number (HH:MM:SS)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • formating Time stored as a number (HH:MM:SS)

    We store time in a numeric field (6,0) (HHMMSS format). Is there a way to force leading zeros in a number when using ".toString". I didn't see anything in Valence APIs for converting a number with leading zeros. The code below works but I didn't know if there was a better method.

    Code:
    var atime = value.toString();
    if (atime.length < 2) {
      atime = '00000' + atime;
    }
    if (atime.length < 3) {
      atime = '0000' + atime;
    }
    if (atime.length < 4) {
      atime = '000' + atime;
    }
    if (atime.length < 5) {
      atime = '00' + atime;
    }
    if (atime.length < 6) {
      atime = '0' + atime;
    }
    return atime.substr(0,2) + ':' + atime.substr(2,2) + ':' + atime.substr(4,2);

  • #2
    You could use padStart however IE doesn't support it. If you need to support IE you could write your own method like below.

    Code:
    function padString(str, size) {
        while (str.length < (size || 2)) {
            str = '0' + str;
        }
        return str;
    }
    
    // example
    // 
    console.log(padString('1', 5));
    
    // expected output in the console would be "00001"
    Thanks

    Comment


    • #3
      Thanks Johnny. That makes sense.

      Comment

      Working...
      X