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());
output:
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))
output :
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"));
output:
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));
output:
date methods getting dates
getDate()
Returns the day of the month (from 1-31)
example:
const d = new Date();
console.log(d.getDate());
result:
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);
output:
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);
math.random() method gives you the number randomly between 0-1.
output:
more properties and methods of math can be seen in mdn docs .
Top comments (0)