DEV Community

Surendrababuvunnam
Surendrababuvunnam

Posted on

Date and math

hey friends today we are gonna learn about date math array and array methods.

Dates

it is a built in JavaScript function to get a date so that it could be used in day to day operations. The most general syntax is shown below

 console.log(Date()); 
Enter fullscreen mode Exit fullscreen mode

output:

Image description

but it is better to use new Date() because it returns a date object which can be used for various operations.

there are four ways of creating a date they are :

new Date(): this is similar to shown in above example

new Date(milliseconds): this give no of milliseconds from Jan 1, 1970

example :

console.log(new Date(10000000000))
Enter fullscreen mode Exit fullscreen mode

output :

Image description

new Date(dateString): When one string argument is passed, it is a string representation of a date.

example:

console.log(new Date("2015-03-25"));
Enter fullscreen mode Exit fullscreen mode

output:

Image description

new Date(DateObject):
this creates a date object with the specified date and time.

example:

console.log(new Date(2023, 1, 10, 16, 33, 30, 0));
Enter fullscreen mode Exit fullscreen mode

output:

Image description

date methods getting dates

getDate()

Returns the day of the month (from 1-31)

example:

const d = new Date();
console.log(d.getDate());
Enter fullscreen mode Exit fullscreen mode

result:

Image description

other examples include getYear() getMonth() etc.

math

it is a built-in object whose purpose is to give user mathematical constants and operations by help of built in functions.

math properties

all the properties and methods of math are static hence they are called as static properties and static methods.

based on browser and operating systems these values can sometimes vary a little bit.

static properties

these are the properites that give you the values of constants. The examples can be seen below.

example:

const mat = Math.PI;
console.log(mat);
Enter fullscreen mode Exit fullscreen mode

output:

Image description

as seen in the above example the above code gets you the value of constant pi.

static methods

these are the methods that return you the value based on the method applied which is seen in the bellow example

example :

const random = Math.random() * 10 ;
console.log(random);

Enter fullscreen mode Exit fullscreen mode

math.random() method gives you the number randomly between 0-1.

output:

Image description

more properties and methods of math can be seen in mdn docs .

Top comments (0)