Time and Stopwatch Object

Posted on
  • This Object implements a Date and Time formatting methods and a stopwatch function.
    TimeObj.js listing follows:

    //The constructor for TimeObj
    function TimeObj(){
      this.Month=1;
      this.Day=1;
      this.Year=0;
      this.Hour=0;
      this.Minute=0;
      this.Second=0;
      this.t=0;
      this.t1=0;
      this.t2=0;
      this.et=0;
    }//end TimeObj
    
    //Methods for TimeObj
    
    TimeObj.prototype.updateTime=function(){­
       //setup date and time
        this.t = new Date();
        this.Month=this.t.getMonth();
        this.Day=this.t.getDate();
        this.Year=this.t.getFullYear();
        this.Hour =this.t.getHours(); 
        this.Minute=("0"+this.t.getMinutes()).su­bstr(-2);
        this.Second= ("0"+this.t.getSeconds()).substr(-2);
    };//end updateTime
    
    // a="M"; b="/"; c="D"; d="/"; e="Y";
    TimeObj.prototype.getDate=function(a,b,c­,d,e){
    var sss="";
      switch (a){
        case 'D': sss+=this.Day;break;
        case 'M': sss+=this.Month;break;
        case 'Y': sss+=this.Year;break;
        default: console.log("getDate Error");
      }//end switch a
      sss+=b;
      switch (c){
        case 'D': sss+=this.Day;break;
        case 'M': sss+=this.Month;break;
        case 'Y': sss+=this.Year;break;
        default: console.log("getDate Error");
      }//end switch c
      sss+=d;
      switch (e){
        case 'D': sss+=this.Day;break;
        case 'M': sss+=this.Month;break;
        case 'Y': sss+=this.Year;break;
        default: console.log("getDate Error");
      }//end switch e
      return sss;
    };//end getDate
    
    // a="H"; b="/"; c="M"; d="/"; e="S"
    TimeObj.prototype.getTime=function(a,b,c­,d,e){
    var sss="";
      switch (a){
        case 'H': sss+=this.Hour;break;
        case 'M': sss+=this.Minute;break;
        case 'S': sss+=this.Second;break;
        default: console.log("getTime Error");
      }//end switch a
      sss+=b;
      switch (c){
        case 'H': sss+=this.Hour;break;
        case 'M': sss+=this.Minute;break;
        case 'S': sss+=this.Second;break;
        default: console.log("getTime Error");
      }//end switch c
      sss+=d;
      switch (e){
        case 'H': sss+=this.Hour;break;
        case 'M': sss+=this.Minute;break;
        case 'S': sss+=this.Second;break;
        default: console.log("getTime Error");
      }//end switch e
      return sss;
    };//end gettTime
    
    // Stopwatch functions
    TimeObj.prototype.SWstart=function(){
      this.updateTime();
      this.t1=this.t;
    };//end SWstart
    
    TimeObj.prototype.SWstop=function(){
      this.updateTime();
      this.t2=this.t;
      this.et=this.t2-this.t1;
      return this.et;
    };//end SWstop
    
    //end Methods for TimeObj
    
    //Code to test TimeObj
    function Testit(){
     var T= new TimeObj();
     T.updateTime();
     T.SWstart();
    var aa=[['D','/','M','/','Y'],['M','/','D','­/','Y'],['Y','/','M','/','D']];
     console.log(T.getDate.apply(T,aa[0]));
     console.log(T.getDate.apply(T,aa[1]));
     console.log(T.getDate.apply(T,aa[2]));
    
    var bb=[['H','/','M','/','S'],['H',':','M','­:','S'],['M','/','S',':','H']];
     console.log(T.getTime.apply(T,bb[0]));
     console.log(T.getTime.apply(T,bb[1]));
     console.log(T.getTime.apply(T,bb[2]));
    
    
     console.log(T.SWstop().toFixed(2));
    }//end Testit
    
    Testit();
    
    

    The output of the Testit()function as seen in the left pane of the WebIDE:

    >echo(0);
    5/4/2016
    4/5/2016
    2016/4/5
    6/59/36
    6:59:36
    59/36:6
    14.58
    =undefined
    >
    
    

    One question is about the elapsed time units. The value 14.58 seems a bit long for seconds. Perhaps it is milliseconds.


    1 Attachment

  • I think if you look at new Date you actually get an object. If you then do maths with that object, Date.valueOf gets called, which returns a time in milliseconds since 1970...

    >new Date()
    ={ "ms": 949363214047.21643066406 }
    >0+(new Date())                                                                 
    =949363248128.48754882812  
    

    So yes, the time returned by SWstop will be milliseconds.

    Hope that helps :)

  • @gordon, I've been using the date constructor and splitting on spaces to format the hh:mm:ss part for a count down timer, and have discovered that if the the time is negative, you end up with a - sign instead of a leading 0.

    I supply sample code if this makes it more clear....

  • >(new Date(-1000)).toString()                                                   
    ="Thu Jan 1 1970 00:00:-1 GMT+0000" 
    

    I've just filed a bug for this

  • What would the correct output be in this case? What does node do?

    -00:00:01 ?

    The output at the moment can put a - in the h, m or s leading slot

  • Let's discuss it in the bug rather than messing up @ClearMemory041063's thread :)

  • http://forum.espruino.com/conversations/­286424/#comment12967777
    Attached is an upgraded module timeobj.js and a project file testTimeObj.js.

    You're correct that new.Date is an object, so the duplications in the object has been removed.
    I found one bug in my code in that months are numbered 0 to 11.
    The format uses arrays. The 'm' produces the three letter month name.

    I tried using local modules today in a project directory, great so far.

    Thanks for the comments and tips.


    2 Attachments

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Time and Stopwatch Object

Posted by Avatar for ClearMemory041063 @ClearMemory041063

Actions