You are reading a single comment by @ClearMemory041063 and its replies. Click here to read the full conversation.
  • 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()).substr(-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

About