Announcement

Collapse
No announcement yet.

question on Date() Object

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

  • question on Date() Object

    Hi all!!
    Can anyone tell me why using this method of the Date() object does not produce a "two digit" printout of the minutes and seconds under the value of 10 ???

    var time = new Date();
    var m = time.getMinutes();
    if (m.length < 2) m= "0"+m;
    var s= time.getSeconds();
    if(s.length < 2) s = "0"+ s;
    var t = m+ ":" + ":" s;
    return t;

    My workaround using the following method produces a 2 digit readout:
    if(m<10) m = "0"+m
    if(s<10) s = "0"+s

    Can I not use ".length" method of an object to determine the length of the output??

    I saw the use of the .length code previously in another program
    but I can't get it to work properly .
    Thanks !!
    angelo
    ang.

  • #2
    Re: question on Date() Object

    angelo
    There isn't a.length method for numbers [which is what m and s are] so you would need to convert m and s to strings prior to checking their lengths. Also you omitted to add a + before the s in the line var t = m+ ":" + ":" s;
    An easier way to accomplish what you want is to just check if the values of m and s are less than 10 in which case you concatenate the leading 0
    Alex


    Originally posted by angatgg
    Hi all!!
    Can anyone tell me why using this method of the Date() object does not produce a "two digit" printout of the minutes and seconds under the value of 10 ???

    var time = new Date();
    var m = time.getMinutes();
    if (m.length < 2) m= "0"+m;
    var s= time.getSeconds();
    if(s.length < 2) s = "0"+ s;
    var t = m+ ":" + ":" s;
    return t;

    My workaround using the following method produces a 2 digit readout:
    if(m<10) m = "0"+m
    if(s<10) s = "0"+s

    Can I not use ".length" method of an object to determine the length of the output??

    I saw the use of the .length code previously in another program
    but I can't get it to work properly .
    Thanks !!
    angelo

    Comment


    • #3
      Date() object

      Alexis, thank you for help explaining why a.length
      doesn't work in this manner.

      angelo
      ang.

      Comment


      • #4
        Re: Date() object

        angelo
        You are welcome
        Alex


        Originally posted by angatgg
        Alexis, thank you for help explaining why a.length
        doesn't work in this manner.

        angelo

        Comment

        Working...
        X