get System Date, get current Date and Time in sapUI5

View.xml :

<Label text=" " id="date" class="head2"/>

 Controller.js :

put these code in the onInit() function :

var today = new Date();
       var dd = today.getDate();
       var mm = today.getMonth()+1; //January is 0!
       var yyyy = today.getFullYear();
       var hour = today.getHours();
       var Minutes = today.getMinutes();
       var Seconds = today.getSeconds();
       var today1 = ( dd+'.'+mm+'.'+yyyy+' '+hour+':'+Minutes+':'+Seconds);
       this.getView().byId("date").setText(today1.valueOf(Text));






==========================================================


date and time with initial 0 :



in this code, we used the if loop to set the initial zero value when the value of minutes is less than 10. 


 var today = new Date();
       var dd = today.getDate();
       var mm = today.getMonth()+1; //January is 0!
       var yyyy = today.getFullYear();
   
       if(dd<10) {

        dd='0'+dd

    }

    if(mm<10) {

        mm='0'+mm

    }
   
       var hour = today.getHours();
       var Minutes = today.getMinutes();
       var Seconds = today.getSeconds();
   
       if(hour<10) {

        hour='0'+hour

    }

    if(Minutes<10) {

        Minutes='0'+Minutes

    }
    if(Seconds<10) {

    Seconds ='0'+Seconds

    }
   
   
       var today1 = ( dd+'.'+mm+'.'+yyyy+' '+hour+':'+Minutes+':'+Seconds);
       this.getView().byId("date").setText(today1.valueOf(Text));



View.xml :

<Label text=" " id="date" class="head2"/>





( check the first and second screenshots seconds value )











Post a Comment

Previous Post Next Post