JavaScript - Date and Math Object

Date Object :

The Date object works with dates and times. Date objects are created with new Date().
Eg :
    < !DOCTYPE html>
    < html>
    < body>

    < h1>JavaScript Dates< /h1>
    < h2>Using new Date()< /h2>
    < p id="demo">< /p>
    < p id="demo1">< /p>

    < script>
        const d = new Date();           //gives curent date and time
        document.getElementById("demo").innerHTML = d;

        const d1 = new Date("2024-10-09");  //gives date value
        document.getElementById("demo1").innerHTML = d1;
    < /script>

    < /body>
    < /html>
Output :

JavaScript Dates

Using new Date()

Wed Oct 09 2024 17:59:12 GMT+0530 (India Standard Time)

Wed Oct 09 2024 05:30:00 GMT+0530 (India Standard Time)



Creating Date Objects :
Date objects are created with the new Date() constructor.There are 9 ways to create a new date object:
    new Date()
    new Date(date string)

    new Date(year,month)
    new Date(year,month,day)
    new Date(year,month,day,hours)
    new Date(year,month,day,hours,minutes)
    new Date(year,month,day,hours,minutes,seconds)
    new Date(year,month,day,hours,minutes,seconds,ms)

    new Date(milliseconds)
new Date(year, month, ...)
new Date(year, month, ...) creates a date object with a specified date and time. 7 numbers specify year, month, day, hour, minute, second, and millisecond (in that order):
    < !DOCTYPE html>
    < html>
    < body>

    < h2>JavaScript new Date()< /h2>
    < p>Using new Date(7 numbers), creates a new date object with the specified date and time:< /p>
    < p id="demo">< /p>

    < script>
        const d = new Date(2024, 10, 09, 06, 02, 30, 0);
        document.getElementById("demo").innerHTML = d;
    < /script>

    <   /body>
    <   /html>
Output :

JavaScript new Date()

Using new Date(7 numbers), creates a new date object with the specified date and time:

Sat Nov 09 2024 06:02:30 GMT+0530 (India Standard Time)



new Date(milliseconds) :
new Date(milliseconds) creates a new date object as milliseconds plus zero time:
    < !DOCTYPE html>
    < html>
    < body>

    < h1>JavaScript Dates< /h1>
    < h2>Using new Date()< /h2>
    < p>1728500000000 milliseconds from January 01 1970 UTC is:< /p>
    < p id="demo">< /p>

    < script>
        const d = new Date(1728500000000);
        document.getElementById("demo").innerHTML = d;
    < /script>

    < /body>
    < /html>
Output :

JavaScript Dates

Using new Date()

1728500000000 milliseconds from January 01 1970 UTC is:

Thu Oct 10 2024 00:23:20 GMT+0530 (India Standard Time)




Math Object :

Now we are going to see about Math.ceil(), Math.floor(), Math.trunc(), Math.sign()

    < !DOCTYPE html>
    < html>
    < body>

    < h2>JavaScript Math.ceil()< /h2>
    < p>Math.ceil() rounds a number up to its nearest integer:< /p>
    < p id="demo">< /p>
    < p id="demo1">< /p>
    < p id="demo2">< /p>
    < p id="demo3">< /p>

    < h2>JavaScript Math.floor()< /h2>
    < p>Math.floor(x) returns the value of x rounded down to its nearest integer:< /p>
    < p id="demoa">< /p>
    < p id="demob">< /p>
    < p id="democ">< /p>
    < p id="demod">< /p>

    < h2>JavaScript Math.trunc()< /h2>
    < p>Math.trunc(x) returns the integer part of x:< /p>
    < p id="demo1a">< /p>
    < p id="demo1b">< /p>
    < p id="demo1c">< /p>
    < p id="demo1d">< /p>

    < h2>Math.sign()< /h2>
    < p>Math.sign(x) returns if x is negative, null or positive:< /p>
    < p id="demo2a">< /p>
    < p id="demo2b">< /p>
    < p id="demo2c">< /p>

    < script>
        document.getElementById("demo").innerHTML = Math.ceil(4.4);
        document.getElementById("demo1").innerHTML = Math.ceil(4.5);
        document.getElementById("demo2").innerHTML = Math.ceil(4.8);
        document.getElementById("demo3").innerHTML = Math.ceil(-4.4);

        document.getElementById("demoa").innerHTML = Math.floor(4.9);
        document.getElementById("demob").innerHTML = Math.floor(4.4);
        document.getElementById("democ").innerHTML = Math.floor(4.2);
        document.getElementById("demod").innerHTML = Math.floor(-4.2);

        document.getElementById("demo1a").innerHTML = Math.trunc(4.9);
        document.getElementById("demo1b").innerHTML = Math.trunc(4.4);
        document.getElementById("demo1c").innerHTML = Math.trunc(4.2);
        document.getElementById("demo1d").innerHTML = Math.trunc(-4.2);

        document.getElementById("demo2a").innerHTML = Math.sign(-4);
        document.getElementById("demo2b").innerHTML = Math.sign(0);
        document.getElementById("demo2c").innerHTML = Math.sign(4);
    < /script>

    < /body>
    < /html>
Output :

JavaScript Math.ceil()

Math.ceil() rounds a number up to its nearest integer:

5

5

5

-4


JavaScript Math.floor()

Math.floor(x) returns the value of x rounded down to its nearest integer:

4

4

4

-5


JavaScript Math.trunc()

Math.trunc(x) returns the integer part of x:

4

4

4

-4



Now we are going to see about Math.pow(), Math.floor, Math.trunc, Math.sign(),Math.min(), Math.max()

    < !DOCTYPE html>
    < html>
    < body>

    < h2>JavaScript Math.pow()< /h2>
    < p>Math.pow(x,y) returns the value of x to the power of y:< /p>
    < p id="demo">< /p>

    < h2>Math.sqrt()< /h2>
    < p>Math.sqrt(x) returns the square root of x:< /p>
    < p id="demo1">< /p>

    < h2>Math.abs()< /h2>
    < p>Math.abs(x) returns the absolute (positive) value of x:< /p>
    < p id="demo2">< /p>

    < h2>Math.sin()< /h2>
    < p>Math.sin(x) returns the sine (a value between -1 and 1) of the angle x (given in radians).
    If you want to use degrees instead of radians, you have to convert degrees to radians:< /p>
    < p>Angle in radians = Angle in degrees x PI / 180.< /p>
    < p id="demo3">< /p>

    < h2>Math.min() and Math.max()< /h2>
    < p>Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments:< /p>
    < p id="demo4">< /p>
    < p id="demo4a">< /p>

    < script>
        document.getElementById("demo").innerHTML = Math.pow(8,2);

        document.getElementById("demo1").innerHTML =Math.sqrt(64);

        document.getElementById("demo2").innerHTML =Math.abs(-4.7);

        document.getElementById("demo3").innerHTML =Math.sin(90 * Math.PI / 180);   // returns 1 (the sine of 90 degrees)

        document.getElementById("demo4").innerHTML =Math.min(0, 150, 30, 20, -8, -200);
        document.getElementById("demo4a").innerHTML =Math.max(0, 150, 30, 20, -8, -200);
    < /script>

    < /body>
    < /html>
Output :

JavaScript Math.pow()

Math.pow(x,y) returns the value of x to the power of y:

64


Math.sqrt()

Math.sqrt(x) returns the square root of x:

8


Math.abs()

Math.abs(x) returns the absolute (positive) value of x:

4.7


Math.sin()

Math.sin(x) returns the sine (a value between -1 and 1) of the angle x (given in radians). If you want to use degrees instead of radians, you have to convert degrees to radians:

Angle in radians = Angle in degrees x PI / 180.

1


Math.min() and Math.max()

Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments:

-200

0.5995704368870243



There are many methods used in Math object

        Method	                                Description
    --------------------    --------------------------------------------------------------------------------
    abs(x)	                Returns the absolute value of x
    acos(x)	                Returns the arccosine of x, in radians
    acosh(x)                    Returns the hyperbolic arccosine of x
    asin(x)	                Returns the arcsine of x, in radians
    asinh(x)	                Returns the hyperbolic arcsine of x
    atan(x)	                Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
    atan2(y, x)	                Returns the arctangent of the quotient of its arguments
    atanh(x)	                Returns the hyperbolic arctangent of x
    cbrt(x)	                Returns the cubic root of x
    ceil(x)	                Returns x, rounded upwards to the nearest integer
    cos(x)	                Returns the cosine of x (x is in radians)
    cosh(x)	                Returns the hyperbolic cosine of x
    exp(x)	                Returns the value of Ex
    floor(x)	                Returns x, rounded downwards to the nearest integer
    log(x)	                Returns the natural logarithm (base E) of x
    max(x, y, z, ..., n)	Returns the number with the highest value
    min(x, y, z, ..., n)	Returns the number with the lowest value
    pow(x, y)	                Returns the value of x to the power of y
    random()	                Returns a random number between 0 and 1
    round(x)	                Rounds x to the nearest integer
    sign(x)	                Returns if x is negative, null or positive (-1, 0, 1)
    sin(x)	                Returns the sine of x (x is in radians)
    sinh(x)	                Returns the hyperbolic sine of x
    sqrt(x)	                Returns the square root of x
    tan(x)	                Returns the tangent of an angle
    tanh(x)	                Returns the hyperbolic tangent of a number
    trunc(x)	                Returns the integer part of a number (x)


(JS - DOM Methods)