Date and Math in js

Table of contents

Date

The JavaScript date object can be used to get year, month and day. You can display a timer on the webpage by the help of JavaScript date object.

You can use 4 way to create date object.

  1. Date()

  2. Date(milliseconds)

  3. Date(dateString)

  4. Date(year, month, day, hours, minutes, seconds, milliseconds)

    For Example:-

     <script>  
     var date=new Date();  
     var day=date.getDate();  
     var month=date.getMonth()+1;  
     var year=date.getFullYear();  
     document.write("<br>Date is: "+day+"/"+month+"/"+year);  
     </script>
    

    or

     // Date 
     let date =new Date();   // show pre-defined date
     console.log(date);
    
     let date1 = new Date(2000,3,18);  //show giveing date
     console.log(date1);
     console.log(date1.getDate());
     console.log(date1.getDay());
     console.log(date1.getFullYear());
    
     let date2 = new Date();
     console.log(date2.getMinutes());
     console.log(date2.getSeconds());
     console.log(date2.toUTCString());   //universal time
    

    Math

    The JavaScript math object provides several constants and methods to perform mathematical operation.

     // Math
     console.log(Math.round(5.4))     //show nearest value like 5 
     console.log(Math.floor(5.9))    //show smallest value
     console.log(Math.ceil(6.3))     //show bigger value always
     console.log(Math.trunc(4.934))  //remove digit after point 
     console.log(Math.pow(2,2) )       //2 raise to power2- (2*2)
     console.log(Math.sqrt(81))    // square root
     console.log(Math.abs(-23))     // positive number return 
     console.log(Math.random()*10) //return random number