0% found this document useful (0 votes)
1 views12 pages

Date and Math in javascript

The document provides an overview of the Math and Date objects in JavaScript, detailing their methods and functionalities. It explains how to perform mathematical tasks using the Math object and how to manipulate dates with the Date object, including creating, displaying, and parsing dates. Key methods for both objects are listed, along with examples of their usage in code.

Uploaded by

aarushsingh3944
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views12 pages

Date and Math in javascript

The document provides an overview of the Math and Date objects in JavaScript, detailing their methods and functionalities. It explains how to perform mathematical tasks using the Math object and how to manipulate dates with the Date object, including creating, displaying, and parsing dates. Key methods for both objects are listed, along with examples of their usage in code.

Uploaded by

aarushsingh3944
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

DATE AND MATH

1
The Math Object
• The Math object allows you to perform mathematical tasks.

• The Math object includes several mathematical methods.

• Examples:
Math.min(0, 150, 30, 20, -8, -200); // returns -200
Math.max(0, 150, 30, 20, -8, -200); // returns 150

• Syntax: var pi_val = Math.PI;


var sine_val = Math.sin(30);

• Math is not a constructor. All the properties and methods of Math


are static and can be called by using Math as an object without
creating it. 2
In – built Math Methods
Method Description
abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x, in radians
asin(x) Returns the arcsine of x, in radians
Returns the arctangent of x as a numeric value between
atan(x)
-PI/2 and PI/2 radians
atan2(y,x) Returns the arctangent of the quotient of its arguments

ceil(x) Returns x, rounded upwards to the nearest integer


cos(x) Returns the cosine of x (x is in radians)
exp(x) Returns the value of Ex
floor(x) Returns x, rounded downwards to the nearest integer
3
In – built Math Methods
Method Description
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


sin(x) Returns the sine of x (x is in radians)
sqrt(x) Returns the square root of x
tan(x) Returns the tangent of an angle
4
The DATE Object
• The Date object lets you work with dates (years, months,
days, hours, minutes, seconds, and milliseconds)

• JavaScript Date Formats


A JavaScript date can be written as a string:
Wed Jul 08 2015 11:29:06 GMT+0700 (SE Asia
Standard Time)
or as a number:
1436329746239

• Dates written as numbers, specifies the number of


milliseconds since January 1, 1970, 00:00:00.

5
Displaying Date
<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>
Input
<script>
document.getElementById("demo").innerHTML =
Date();
</script>

</body>
</html>

Output Wed Jul 08 2015 11:32:51 GMT+0700 (SE Asia Standard Time)

6
Creating Date Objects
• Date objects are created with the new Date()
constructor.

• There are 4 ways of initiating a date:

• new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds,
milliseconds)

• Using new Date(), creates a new date object with the


current date and time.
7
Date get Methods
Method Description
getDate() Get the day as a number (1-31)
getDay() Get the weekday as a number (0-6)
getFullYear() Get the four digit year (yyyy)
getHours() Get the hour (0-23)
getMilliseconds() Get the milliseconds (0-999)
getMinutes() Get the minutes (0-59)
getMonth() Get the month (0-11)
getSeconds() Get the seconds (0-59)
Get the time (milliseconds since January 1,
getTime()
1970)
8
Examples
• The getTime() Method: getTime() returns the
number of milliseconds since 01.01.1970.
<script>
var d = new Date();
document.getElementById("demo").innerHTML = d.getTime();
</script>

• The getFullYear() Method: getFullYear() returns


the year of a date as a four digit number.
<script>
var d = new Date();
document.getElementById("demo").innerHTML = d.getFullYear();
</script>

9
Date set Methods
Method Description
setDate() Set the day as a number (1-31)
setFullYear() Set the year (optionally month and day)
setHours() Set the hour (0-23)
setMilliseconds() Set the milliseconds (0-999)
setMinutes() Set the minutes (0-59)
setMonth() Set the month (0-11)
setSeconds() Set the seconds (0-59)
Set the time (milliseconds since January
setTime()
1, 1970)
10
Examples
• The setFullYear() Method: setFullYear() sets a
date object to a specific date. In this example, to
January 14, 2020.
<script>
var d = new Date();
d.setFullYear(2020, 0, 14);
document.getElementById("demo").innerHTML = d;
</script>

• The setDate() Method: setDate() sets the day of


the month (1-31).
<script>
var d = new Date();
d.setDate(20);
document.getElementById("demo").innerHTML = d;
</script>

11
Date Input - Parsing Dates
• If you have a valid date string, you can use the
Date.parse() method to convert it to milliseconds.

• Date.parse() returns the number of milliseconds


between the date and January 1, 1970.

<script>
var msec = Date.parse("March 21, 2012");
document.getElementById("demo").innerHTML = msec;
</script>
12

You might also like