Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 931231 of Numbers and dates

  • Revision slug: Web/JavaScript/Guide/Numbers_and_dates
  • Revision title: Numbers and dates
  • Revision id: 931231
  • Created:
  • Creator: nasifmdtanjim
  • Is current revision? No
  • Comment
Tags: 

Revision Content

{{jsSidebar("JavaScript Guide")}} {{PreviousNext("Web/JavaScript/Guide/Expressions_and_Operators", "Web/JavaScript/Guide/Text_formatting")}}

This chapter introduces how to work with numbers and dates in JavaScript.

Numbers

In JavaScript, all numbers are implemented in double-precision 64-bit binary format IEEE 754 (i.e. a number between -(253 -1) and 253 -1). There is no specific type for integers. In addition to being able to represent floating-point numbers, the number type has three symbolic values: +{{jsxref("Infinity")}}, -{{jsxref("Infinity")}}, and {{jsxref("NaN")}} (not-a-number). See also JavaScript data types and structures for context with other primitive types in JavaScript.

You can use four types of number literals: decimal, binary, octal, and hexadecimal.

Decimal numbers

1234567890
42

// Caution when using leading zeros:

0888 // 888 parsed as decimal
0777 // parsed as octal in non-strict mode (511 in decimal)

Note that decimal literals can start with a zero (0) followed by another decimal digit, but If the next digit after the leading 0 is smaller than 8, the number gets parsed as an octal number.

Binary numbers

Binary number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "B" (0b or 0B). If the digits after the 0b are not 0 or 1, the following SyntaxError is thrown: "Missing binary digits after 0b".

var FLT_SIGNBIT  = 0b10000000000000000000000000000000; // 2147483648
var FLT_EXPONENT = 0b01111111100000000000000000000000; // 2139095040
var FLT_MANTISSA = 0B00000000011111111111111111111111; // 8388607

Octal numbers

Octal number syntax uses a leading zero. If the digits after the 0 are outside the range 0 through 7, the number will be interpreted as a decimal number.

var n = 0755; // 493
var m = 0644; // 420

Strict mode in ECMAScript 5 forbids octal syntax. Octal syntax isn't part of ECMAScript 5, but it's supported in all browsers by prefixing the octal number with a zero: 0644 === 420 and"\045" === "%". In ECMAScript 6 Octal number is supported with by prefixing a number with "0o". i.e. 

var a = 0o10; // ES6: Octal

Hexadecimal numbers

Hexadecimal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "X" (0x or 0X). If the digits after 0x are outside the range (0123456789ABCDEF),  the following SyntaxError is thrown: "Identifier starts immediately after numeric literal".

0xFFFFFFFFFFFFFFFFF // 295147905179352830000
0x123456789ABCDEF   // 81985529216486900
0XA                 // 10

Number object

The built-in {{jsxref("Number")}} object has properties for numerical constants, such as maximum value, not-a-number, and infinity. You cannot change the values of these properties and you use them as follows:

var biggestNum = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var infiniteNum = Number.POSITIVE_INFINITY;
var negInfiniteNum = Number.NEGATIVE_INFINITY;
var notANum = Number.NaN;

You always refer to a property of the predefined Number object as shown above, and not as a property of a Number object you create yourself.

The following table summarizes the Number object's properties.

Properties of Number
Property Description
{{jsxref("Number.MAX_VALUE")}} The largest representable number
{{jsxref("Number.MIN_VALUE")}} The smallest representable number
{{jsxref("Number.NaN")}} Special "not a number" value
{{jsxref("Number.NEGATIVE_INFINITY")}} Special negative infinite value; returned on overflow
{{jsxref("Number.POSITIVE_INFINITY")}} Special positive infinite value; returned on overflow
{{jsxref("Number.EPSILON")}} Difference between one and the smallest value greater than one that can be represented as a {{jsxref("Number")}}.
{{jsxref("Number.MIN_SAFE_INTEGER")}} Minimum safe integer in JavaScript.
{{jsxref("Number.MAX_SAFE_INTEGER")}} Maximum safe integer in JavaScript.
Methods of Number
Method Description
{{jsxref("Number.parseFloat()")}} Parses a string argument and returns a floating point number.
Same as the global {{jsxref("parseFloat", "parseFloat()")}} function.
{{jsxref("Number.parseInt()")}} Parses a string argument and returns an integer of the specified radix or base.
Same as the global {{jsxref("parseInt", "parseInt()")}} function.
{{jsxref("Number.isFinite()")}} Determines whether the passed value is a finite number.
{{jsxref("Number.isInteger()")}} Determines whether the passed value is an integer.
{{jsxref("Number.isNaN()")}} Determines whether the passed value is {{jsxref("Global_Objects/NaN", "NaN")}}. More robust version of the original global {{jsxref("Global_Objects/isNaN", "isNaN()")}}.
{{jsxref("Number.isSafeInteger()")}} Determines whether the provided value is a number that is a safe integer.

The Number prototype provides methods for retrieving information from Number objects in various formats. The following table summarizes the methods of Number.prototype.

Methods of Number.prototype
Method Description
{{jsxref("Number.toExponential", "toExponential()")}} Returns a string representing the number in exponential notation.
{{jsxref("Number.toFixed", "toFixed()")}} Returns a string representing the number in fixed-point notation.
{{jsxref("Number.toPrecision", "toPrecision()")}} Returns a string representing the number to a specified precision in fixed-point notation.

Math object

The built-in {{jsxref("Math")}} object has properties and methods for mathematical constants and functions. For example, the Math object's PI property has the value of pi (3.141...), which you would use in an application as

Math.PI

Similarly, standard mathematical functions are methods of Math. These include trigonometric, logarithmic, exponential, and other functions. For example, if you want to use the trigonometric function sine, you would write

Math.sin(1.56)

Note that all trigonometric methods of Math take arguments in radians.

The following table summarizes the Math object's methods.

Methods of Math
Method Description
{{jsxref("Math.abs", "abs()")}} Absolute value
{{jsxref("Math.sin", "sin()")}}, {{jsxref("Math.cos", "cos()")}}, {{jsxref("Math.tan", "tan()")}} Standard trigonometric functions; argument in radians
{{jsxref("Math.asin", "asin()")}}, {{jsxref("Math.acos", "acos()")}}, {{jsxref("Math.atan", "atan()")}}, {{jsxref("Math.atan2", "atan2()")}} Inverse trigonometric functions; return values in radians
{{jsxref("Math.sinh", "sinh()")}}, {{jsxref("Math.cosh", "cosh()")}}, {{jsxref("Math.tanh", "tanh()")}} Hyperbolic trigonometric functions; return values in radians.
{{jsxref("Math.asinh", "asinh()")}}, {{jsxref("Math.acosh", "acosh()")}}, {{jsxref("Math.atanh", "atanh()")}} Inverse hyperbolic trigonometric functions; return values in radians.

{{jsxref("Math.pow", "pow()")}}, {{jsxref("Math.exp", "exp()")}}, {{jsxref("Math.expm1", "expm1()")}}, {{jsxref("Math.log10", "log10()")}}, {{jsxref("Math.log1p", "log1p()")}}, {{jsxref("Math.log2", "log2()")}}

Exponential and logarithmic functions.
{{jsxref("Math.floor", "floor()")}}, {{jsxref("Math.ceil", "ceil()")}} Returns largest/smallest integer less/greater than or equal to argument
{{jsxref("Math.min", "min()")}}, {{jsxref("Math.max", "max()")}} Returns lesser or greater (respectively) of comma separated list of numbers arguments
{{jsxref("Math.random", "random()")}} Returns a random number between 0 and 1.
{{jsxref("Math.round", "round()")}}, {{jsxref("Math.fround", "fround()")}}, {{jsxref("Math.trunc", "trunc()")}}, Rounding and truncation functions.
{{jsxref("Math.sqrt", "sqrt()")}}, {{jsxref("Math.cbrt", "cbrt()")}}, {{jsxref("Math.hypot", "hypot()")}} Square root, cube root, Square root of the sum of square arguments.
{{jsxref("Math.sign", "sign()")}} The sign of a number, indicating whether the number is positive, negative or zero.
{{jsxref("Math.clz32", "clz32()")}},
{{jsxref("Math.imul", "imul()")}}
Number of leading zero bits in the 32-bit binary representation.
The result of the C-like 32-bit multiplication of the two arguments.

Unlike many other objects, you never create a Math object of your own. You always use the built-in Math object.

Date object

JavaScript does not have a date data type. However, you can use the {{jsxref("Date")}} object and its methods to work with dates and times in your applications. The Date object has a large number of methods for setting, getting, and manipulating dates. It does not have any properties.

JavaScript handles dates similarly to Java. The two languages have many of the same date methods, and both languages store dates as the number of milliseconds since January 1, 1970, 00:00:00.

The Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.

To create a Date object:

var dateObjectName = new Date([parameters]);

where dateObjectName is the name of the Date object being created; it can be a new object or a property of an existing object.

Calling Date without the new keyword simply converts the provided date to a string representation.

The parameters in the preceding syntax can be any of the following:

  • Nothing: creates today's date and time. For example, today = new Date();.
  • A string representing a date in the following form: "Month day, year hours:minutes:seconds." For example, var Xmas95 = new Date("December 25, 1995 13:30:00"). If you omit hours, minutes, or seconds, the value will be set to zero.
  • A set of integer values for year, month, and day. For example, var Xmas95 = new Date(1995, 11, 25).
  • A set of integer values for year, month, day, hour, minute, and seconds. For example, var Xmas95 = new Date(1995, 11, 25, 9, 30, 0);.

Methods of the Date object

The Date object methods for handling dates and times fall into these broad categories:

  • "set" methods, for setting date and time values in Date objects.
  • "get" methods, for getting date and time values from Date objects.
  • "to" methods, for returning string values from Date objects.
  • parse and UTC methods, for parsing Date strings.

With the "get" and "set" methods you can get and set seconds, minutes, hours, day of the month, day of the week, months, and years separately. There is a getDay method that returns the day of the week, but no corresponding setDay method, because the day of the week is set automatically. These methods use integers to represent these values as follows:

  • Seconds and minutes: 0 to 59
  • Hours: 0 to 23
  • Day: 0 (Sunday) to 6 (Saturday)
  • Date: 1 to 31 (day of the month)
  • Months: 0 (January) to 11 (December)
  • Year: years since 1900

For example, suppose you define the following date:

var Xmas95 = new Date("December 25, 1995");

Then Xmas95.getMonth() returns 11, and Xmas95.getFullYear() returns 1995.

The getTime and setTime methods are useful for comparing dates. The getTime method returns the number of milliseconds since January 1, 1970, 00:00:00 for a Date object.

For example, the following code displays the number of days left in the current year:

var today = new Date();
var endYear = new Date(1995, 11, 31, 23, 59, 59, 999); // Set day and month
endYear.setFullYear(today.getFullYear()); // Set year to this year
var msPerDay = 24 * 60 * 60 * 1000; // Number of milliseconds per day
var daysLeft = (endYear.getTime() - today.getTime()) / msPerDay;
var daysLeft = Math.round(daysLeft); //returns days left in the year

This example creates a Date object named today that contains today's date. It then creates a Date object named endYear and sets the year to the current year. Then, using the number of milliseconds per day, it computes the number of days between today and endYear, using getTime and rounding to a whole number of days.

The parse method is useful for assigning values from date strings to existing Date objects. For example, the following code uses parse and setTime to assign a date value to the IPOdate object:

var IPOdate = new Date();
IPOdate.setTime(Date.parse("Aug 9, 1995"));

Example

In the following example, the function JSClock() returns the time in the format of a digital clock.

function JSClock() {
  var time = new Date();
  var hour = time.getHours();
  var minute = time.getMinutes();
  var second = time.getSeconds();
  var temp = "" + ((hour > 12) ? hour - 12 : hour);
  if (hour == 0)
    temp = "12";
  temp += ((minute < 10) ? ":0" : ":") + minute;
  temp += ((second < 10) ? ":0" : ":") + second;
  temp += (hour >= 12) ? " P.M." : " A.M.";
  return temp;
}

The JSClock function first creates a new Date object called time; since no arguments are given, time is created with the current date and time. Then calls to the getHours, getMinutes, and getSeconds methods assign the value of the current hour, minute, and second to hour, minute, and second.

The next four statements build a string value based on the time. The first statement creates a variable temp, assigning it a value using a conditional expression; if hour is greater than 12, (hour - 12), otherwise simply hour, unless hour is 0, in which case it becomes 12.

The next statement appends a minute value to temp. If the value of minute is less than 10, the conditional expression adds a string with a preceding zero; otherwise it adds a string with a demarcating colon. Then a statement appends a seconds value to temp in the same way.

Finally, a conditional expression appends "P.M." to temp if hour is 12 or greater; otherwise, it appends "A.M." to temp.

{{PreviousNext("Web/JavaScript/Guide/Expressions_and_Operators", "Web/JavaScript/Guide/Text_formatting")}}

Revision Source

<div>{{jsSidebar("JavaScript Guide")}} {{PreviousNext("Web/JavaScript/Guide/Expressions_and_Operators", "Web/JavaScript/Guide/Text_formatting")}}</div>

<p class="summary">This chapter introduces how to work with numbers and dates in JavaScript.</p>

<h2 id="Numbers">Numbers</h2>

<p>In JavaScript, all numbers are implemented in <a class="external external-icon" href="https://en.wikipedia.org/wiki/Double-precision_floating-point_format">double-precision 64-bit binary format IEEE 754</a> (i.e. a number between -(2<sup>53</sup> -1) and 2<sup>53</sup> -1). <strong>There is no specific type for integers</strong>. In addition to being able to represent floating-point numbers, the number type has three symbolic values: <code>+</code>{{jsxref("Infinity")}}, <code>-</code>{{jsxref("Infinity")}}, and {{jsxref("NaN")}} (not-a-number). See also <a href="/en-US/docs/Web/JavaScript/Data_structures">JavaScript data types and structures</a> for context with other primitive types in JavaScript.</p>

<p>You can use four types of number literals: decimal, binary, octal, and hexadecimal.</p>

<h3 id="Decimal_numbers">Decimal numbers</h3>

<pre class="brush: js">
1234567890
42

// Caution when using leading zeros:

0888 // 888 parsed as decimal
0777 // parsed as octal in non-strict mode (511 in decimal)
</pre>

<p>Note that decimal literals can start with a zero (<code>0</code>) followed by another decimal digit, but If the next digit after the leading <code>0</code> is smaller than 8, the number gets parsed as an octal number.</p>

<h3 id="Binary_numbers">Binary numbers</h3>

<p>Binary number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "B" (<code>0b</code> or <code>0B</code>). If the digits after the <code>0b</code> are not 0 or 1, the following&nbsp;<code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError">SyntaxError</a></code>&nbsp;is thrown: "Missing binary digits after 0b".</p>

<pre class="brush: js">
var FLT_SIGNBIT  = 0b10000000000000000000000000000000; // 2147483648
var FLT_EXPONENT = 0b01111111100000000000000000000000; // 2139095040
var FLT_MANTISSA = 0B00000000011111111111111111111111; // 8388607</pre>

<h3 id="Octal_numbers">Octal numbers</h3>

<p>Octal number syntax uses a leading zero. If the digits after the <code>0</code>&nbsp;are outside the range 0 through 7,&nbsp;the number will be interpreted as a decimal number.</p>

<pre class="brush: js">
var n = 0755; // 493
var m = 0644; // 420
</pre>

<p>Strict mode in ECMAScript 5&nbsp;forbids octal syntax. Octal syntax isn't part of ECMAScript 5, but it's supported in all browsers by prefixing the octal number with a zero:&nbsp;<code>0644 === 420</code>&nbsp;and<code>"\045" === "%"</code>. In ECMAScript 6 Octal number is supported with by prefixing a number with "<code>0</code>o". i.e.&nbsp;</p>

<pre class="brush: js" data-number="">
<code>var a = 0o10; // ES6: Octal</code></pre>

<h3 id="Hexadecimal_numbers">Hexadecimal numbers</h3>

<p>Hexadecimal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "X" (<code>0x</code> or <code>0X)</code>. If the digits after 0x are outside the range&nbsp;(0123456789ABCDEF),&nbsp;&nbsp;the following&nbsp;<code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError">SyntaxError</a></code>&nbsp;is thrown: "Identifier starts immediately after numeric literal".</p>

<pre class="brush: js">
0xFFFFFFFFFFFFFFFFF // 295147905179352830000
0x123456789ABCDEF   // 81985529216486900
0XA                 // 10
</pre>

<h2 id="Number_object"><code>Number</code> object</h2>

<p>The built-in {{jsxref("Number")}} object has properties for numerical constants, such as maximum value, not-a-number, and infinity. You cannot change the values of these properties and you use them as follows:</p>

<pre class="brush: js">
var biggestNum = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var infiniteNum = Number.POSITIVE_INFINITY;
var negInfiniteNum = Number.NEGATIVE_INFINITY;
var notANum = Number.NaN;
</pre>

<p>You always refer to a property of the predefined <code>Number</code> object as shown above, and not as a property of a <code>Number</code> object you create yourself.</p>

<p>The following table summarizes the <code>Number</code> object's properties.</p>

<table class="standard-table">
 <caption>Properties of <code>Number</code></caption>
 <thead>
  <tr>
   <th scope="col">Property</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{jsxref("Number.MAX_VALUE")}}</td>
   <td>The largest representable number</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.MIN_VALUE")}}</td>
   <td>The smallest representable number</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.NaN")}}</td>
   <td>Special "not a number" value</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.NEGATIVE_INFINITY")}}</td>
   <td>Special negative infinite value; returned on overflow</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.POSITIVE_INFINITY")}}</td>
   <td>Special positive infinite value; returned on overflow</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.EPSILON")}}</td>
   <td>Difference between one and the smallest value greater than one that can be represented as a {{jsxref("Number")}}.</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.MIN_SAFE_INTEGER")}}</td>
   <td>Minimum safe integer in JavaScript.</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.MAX_SAFE_INTEGER")}}</td>
   <td>Maximum safe integer in JavaScript.</td>
  </tr>
 </tbody>
</table>

<table class="standard-table">
 <caption>Methods of <code>Number</code></caption>
 <thead>
  <tr>
   <th>Method</th>
   <th>Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{jsxref("Number.parseFloat()")}}</td>
   <td>Parses a string argument and returns a floating point number.<br />
    Same as the global {{jsxref("parseFloat", "parseFloat()")}} function.</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.parseInt()")}}</td>
   <td>Parses a string argument and returns an integer of the specified radix or base.<br />
    Same as the global {{jsxref("parseInt", "parseInt()")}} function.</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.isFinite()")}}</td>
   <td>Determines whether the passed value is a finite number.</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.isInteger()")}}</td>
   <td>Determines whether the passed value is an integer.</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.isNaN()")}}</td>
   <td>Determines whether the passed value is {{jsxref("Global_Objects/NaN", "NaN")}}. More robust version of the original global {{jsxref("Global_Objects/isNaN", "isNaN()")}}.</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.isSafeInteger()")}}</td>
   <td>Determines whether the provided value is a number that is a <dfn>safe integer</dfn>.</td>
  </tr>
 </tbody>
</table>

<p>The <code>Number</code> prototype provides methods for retrieving information from <code>Number</code> objects in various formats. The following table summarizes the methods of <code>Number.prototype</code>.</p>

<table class="standard-table">
 <caption>Methods of <code>Number.prototype</code></caption>
 <thead>
  <tr>
   <th scope="col">Method</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{jsxref("Number.toExponential", "toExponential()")}}</td>
   <td>Returns a string representing the number in exponential notation.</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.toFixed", "toFixed()")}}</td>
   <td>Returns a string representing the number in fixed-point notation.</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.toPrecision", "toPrecision()")}}</td>
   <td>Returns a string representing the number to a specified precision in fixed-point notation.</td>
  </tr>
 </tbody>
</table>

<h2 id="Math_object"><code>Math</code> object</h2>

<p>The built-in {{jsxref("Math")}} object has properties and methods for mathematical constants and functions. For example, the <code>Math</code> object's <code>PI</code> property has the value of pi (3.141...), which you would use in an application as</p>

<pre class="brush: js">
Math.PI
</pre>

<p>Similarly, standard mathematical functions are methods of <code>Math</code>. These include trigonometric, logarithmic, exponential, and other functions. For example, if you want to use the trigonometric function sine, you would write</p>

<pre class="brush: js">
Math.sin(1.56)
</pre>

<p>Note that all trigonometric methods of <code>Math</code> take arguments in radians.</p>

<p>The following table summarizes the <code>Math</code> object's methods.</p>

<table class="standard-table">
 <caption>Methods of <code>Math</code></caption>
 <thead>
  <tr>
   <th scope="col">Method</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{jsxref("Math.abs", "abs()")}}</td>
   <td>Absolute value</td>
  </tr>
  <tr>
   <td>{{jsxref("Math.sin", "sin()")}}, {{jsxref("Math.cos", "cos()")}}, {{jsxref("Math.tan", "tan()")}}</td>
   <td>Standard trigonometric functions; argument in radians</td>
  </tr>
  <tr>
   <td>{{jsxref("Math.asin", "asin()")}}, {{jsxref("Math.acos", "acos()")}}, {{jsxref("Math.atan", "atan()")}}, {{jsxref("Math.atan2", "atan2()")}}</td>
   <td>Inverse trigonometric functions; return values in radians</td>
  </tr>
  <tr>
   <td>{{jsxref("Math.sinh", "sinh()")}}, {{jsxref("Math.cosh", "cosh()")}}, {{jsxref("Math.tanh", "tanh()")}}</td>
   <td>Hyperbolic trigonometric functions; return values in radians.</td>
  </tr>
  <tr>
   <td>{{jsxref("Math.asinh", "asinh()")}}, {{jsxref("Math.acosh", "acosh()")}}, {{jsxref("Math.atanh", "atanh()")}}</td>
   <td>Inverse hyperbolic trigonometric functions; return values in radians.</td>
  </tr>
  <tr>
   <td>
    <p>{{jsxref("Math.pow", "pow()")}}, {{jsxref("Math.exp", "exp()")}},&nbsp;{{jsxref("Math.expm1", "expm1()")}}, {{jsxref("Math.log10", "log10()")}}, {{jsxref("Math.log1p", "log1p()")}}, {{jsxref("Math.log2", "log2()")}}</p>
   </td>
   <td>Exponential and logarithmic functions.</td>
  </tr>
  <tr>
   <td>{{jsxref("Math.floor", "floor()")}}, {{jsxref("Math.ceil", "ceil()")}}</td>
   <td>Returns largest/smallest integer less/greater than or equal to argument</td>
  </tr>
  <tr>
   <td>{{jsxref("Math.min", "min()")}}, {{jsxref("Math.max", "max()")}}</td>
   <td>Returns lesser or greater (respectively) of comma separated list of numbers arguments</td>
  </tr>
  <tr>
   <td>{{jsxref("Math.random", "random()")}}</td>
   <td>Returns a random number between 0 and 1.</td>
  </tr>
  <tr>
   <td>{{jsxref("Math.round", "round()")}}, {{jsxref("Math.fround", "fround()")}}, {{jsxref("Math.trunc", "trunc()")}},</td>
   <td>Rounding and truncation functions.</td>
  </tr>
  <tr>
   <td>{{jsxref("Math.sqrt", "sqrt()")}}, {{jsxref("Math.cbrt", "cbrt()")}}, {{jsxref("Math.hypot", "hypot()")}}</td>
   <td>Square root, cube root, Square root of the sum of square arguments.</td>
  </tr>
  <tr>
   <td>{{jsxref("Math.sign", "sign()")}}</td>
   <td>The sign of a number, indicating whether the number is positive, negative or zero.</td>
  </tr>
  <tr>
   <td>{{jsxref("Math.clz32", "clz32()")}},<br />
    {{jsxref("Math.imul", "imul()")}}</td>
   <td>Number of leading zero bits in the 32-bit binary representation.<br />
    The result of the C-like 32-bit multiplication of the two arguments.</td>
  </tr>
 </tbody>
</table>

<p>Unlike many other objects, you never create a <code>Math</code> object of your own. You always use the built-in <code>Math</code> object.</p>

<h2 id="Date_object"><code>Date</code> object</h2>

<p>JavaScript does not have a date data type. However, you can use the {{jsxref("Date")}} object and its methods to work with dates and times in your applications. The <code>Date</code> object has a large number of methods for setting, getting, and manipulating dates. It does not have any properties.</p>

<p>JavaScript handles dates similarly to Java. The two languages have many of the same date methods, and both languages store dates as the number of milliseconds since January 1, 1970, 00:00:00.</p>

<p>The <code>Date</code> object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.</p>

<p>To create a <code>Date</code> object:</p>

<pre class="brush: js">
var dateObjectName = new Date([parameters]);
</pre>

<p>where <code>dateObjectName</code> is the name of the <code>Date</code> object being created; it can be a new object or a property of an existing object.</p>

<p>Calling <code>Date</code> without the <code>new</code> keyword simply converts the provided date to a string representation.</p>

<p>The <code>parameters</code> in the preceding syntax can be any of the following:</p>

<ul>
 <li>Nothing: creates today's date and time. For example, <code>today = new Date();</code>.</li>
 <li>A string representing a date in the following form: "Month day, year hours:minutes:seconds." For example, <code>var Xmas95 = new Date("December 25, 1995 13:30:00")</code>. If you omit hours, minutes, or seconds, the value will be set to zero.</li>
 <li>A set of integer values for year, month, and day. For example, <code>var Xmas95 = new Date(1995, 11, 25)</code>.</li>
 <li>A set of integer values for year, month, day, hour, minute, and seconds. For example, <code>var Xmas95 = new Date(1995, 11, 25, 9, 30, 0);</code>.</li>
</ul>

<h3 id="Methods_of_the_Date_object">Methods of the <code>Date</code> object</h3>

<p>The <code>Date</code> object methods for handling dates and times fall into these broad categories:</p>

<ul>
 <li>"set" methods, for setting date and time values in <code>Date</code> objects.</li>
 <li>"get" methods, for getting date and time values from <code>Date</code> objects.</li>
 <li>"to" methods, for returning string values from <code>Date</code> objects.</li>
 <li>parse and UTC methods, for parsing <code>Date</code> strings.</li>
</ul>

<p>With the "get" and "set" methods you can get and set seconds, minutes, hours, day of the month, day of the week, months, and years separately. There is a <code>getDay</code> method that returns the day of the week, but no corresponding <code>setDay</code> method, because the day of the week is set automatically. These methods use integers to represent these values as follows:</p>

<ul>
 <li>Seconds and minutes: 0 to 59</li>
 <li>Hours: 0 to 23</li>
 <li>Day: 0 (Sunday) to 6 (Saturday)</li>
 <li>Date: 1 to 31 (day of the month)</li>
 <li>Months: 0 (January) to 11 (December)</li>
 <li>Year: years since 1900</li>
</ul>

<p>For example, suppose you define the following date:</p>

<pre class="brush: js">
var Xmas95 = new Date("December 25, 1995");
</pre>

<p>Then <code>Xmas95.getMonth()</code> returns 11, and <code>Xmas95.getFullYear()</code> returns 1995.</p>

<p>The <code>getTime</code> and <code>setTime</code> methods are useful for comparing dates. The <code>getTime</code> method returns the number of milliseconds since January 1, 1970, 00:00:00 for a <code>Date</code> object.</p>

<p>For example, the following code displays the number of days left in the current year:</p>

<pre class="brush: js">
var today = new Date();
var endYear = new Date(1995, 11, 31, 23, 59, 59, 999); // Set day and month
endYear.setFullYear(today.getFullYear()); // Set year to this year
var msPerDay = 24 * 60 * 60 * 1000; // Number of milliseconds per day
var daysLeft = (endYear.getTime() - today.getTime()) / msPerDay;
var daysLeft = Math.round(daysLeft); //returns days left in the year
</pre>

<p>This example creates a <code>Date</code> object named <code>today</code> that contains today's date. It then creates a <code>Date</code> object named <code>endYear</code> and sets the year to the current year. Then, using the number of milliseconds per day, it computes the number of days between <code>today</code> and <code>endYear</code>, using <code>getTime</code> and rounding to a whole number of days.</p>

<p>The <code>parse</code> method is useful for assigning values from date strings to existing <code>Date</code> objects. For example, the following code uses <code>parse</code> and <code>setTime</code> to assign a date value to the <code>IPOdate</code> object:</p>

<pre class="brush: js">
var IPOdate = new Date();
IPOdate.setTime(Date.parse("Aug 9, 1995"));
</pre>

<h3 id="Example">Example</h3>

<p>In the following example, the function <code>JSClock()</code> returns the time in the format of a digital clock.</p>

<pre class="brush: js">
function JSClock() {
  var time = new Date();
  var hour = time.getHours();
  var minute = time.getMinutes();
  var second = time.getSeconds();
  var temp = "" + ((hour &gt; 12) ? hour - 12 : hour);
  if (hour == 0)
    temp = "12";
  temp += ((minute &lt; 10) ? ":0" : ":") + minute;
  temp += ((second &lt; 10) ? ":0" : ":") + second;
  temp += (hour &gt;= 12) ? " P.M." : " A.M.";
  return temp;
}
</pre>

<p>The <code>JSClock</code> function first creates a new <code>Date</code> object called <code>time</code>; since no arguments are given, time is created with the current date and time. Then calls to the <code>getHours</code>, <code>getMinutes</code>, and <code>getSeconds</code> methods assign the value of the current hour, minute, and second to <code>hour</code>, <code>minute</code>, and <code>second</code>.</p>

<p>The next four statements build a string value based on the time. The first statement creates a variable <code>temp</code>, assigning it a value using a conditional expression; if <code>hour</code> is greater than 12, (<code>hour - 12</code>), otherwise simply hour, unless hour is 0, in which case it becomes 12.</p>

<p>The next statement appends a <code>minute</code> value to <code>temp</code>. If the value of <code>minute</code> is less than 10, the conditional expression adds a string with a preceding zero; otherwise it adds a string with a demarcating colon. Then a statement appends a seconds value to <code>temp</code> in the same way.</p>

<p>Finally, a conditional expression appends "P.M." to <code>temp</code> if <code>hour</code> is 12 or greater; otherwise, it appends "A.M." to <code>temp</code>.</p>

<p>{{PreviousNext("Web/JavaScript/Guide/Expressions_and_Operators", "Web/JavaScript/Guide/Text_formatting")}}</p>
Revert to this revision