uTools-Manuals/docs/javascript/Guide/Numbers_and_dates.html
2019-04-21 11:50:48 +08:00

323 lines
25 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div> <div class="prevnext" style="text-align: right;">
<p><a href="Guide/Expressions_and_Operators" style="float: left;">« 上一页</a><a href="Guide/Text_formatting">下一页 »</a></p>
</div></div>
<p class="summary">本章节介绍如何掌握Javascript里的数字和日期类型</p>
<h2 id="数字">数字</h2>
<p>在 JavaScript 里面,数字均为双精度浮点类型<a class="external" href="https://en.wikipedia.org/wiki/Double-precision_floating-point_format" rel="noopener">double-precision 64-bit binary format IEEE 754</a> 也就是说一个介于±2<sup>1023</sup>和±2<sup>+1024</sup>之间的数字或者大约为±10308到±10+308数字精度为53位。整数数据在运算完毕后其值在±(2<sup>53 </sup> 1)内 可以认为是准确的,超出此范围后,数据已无法保证正确性<strong></strong>除了能够表示浮点数,数字类型也还能表示三种符号值: <code>+</code><a href="Reference/Global_Objects/Infinity" title="全局属性 Infinity 是一个数值,表示无穷大。"><code>Infinity</code></a>(正无穷)、<code>-</code><a href="Reference/Global_Objects/Infinity" title="全局属性 Infinity 是一个数值,表示无穷大。"><code>Infinity</code></a>(负无穷)和 <a href="Reference/Global_Objects/NaN" title="全局属性 NaN 的值表示不是一个数字Not-A-Number。"><code>NaN</code></a> (not-a-number非数字)。请参见Javascript指南中的 <a href="https://developer.mozilla.orgData_structures">JavaScript 数据类型和数据结构</a> ,了解其他更多的基本类型。</p>
<p>您可以使用四种数字进制:十进制,二进制,八进制和十六进制。</p>
<h3 id="十进制数字(Decimal_numbers)">十进制数字(Decimal numbers)</h3>
<pre>1234567890
42
// 以零开头的数字的注意事项:
0888 // 888 将被当做十进制处理
0777 // 在非严格格式下会被当做八进制处理 (用十进制表示就是511)
</code></pre>
<p>请注意十进制可以以0开头后面接其他十进制数字但是假如下一个接的十进制数字小于8那么该数字将会被当做八进制处理。</p>
<h3 id="二进制数字(Binary_numbers)">二进制数字(Binary numbers)</h3>
<p>二进制数字语法是以零为开头后面接一个小写或大写的拉丁文字母B(<code>0b或者是0B</code>)。 假如0b后面的数字不是0或者1那么就会提示这样的语法错误 <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError">SyntaxError</a></code> "Missing binary digits after 0b(0b之后缺失二有效的二进制数据)"。</p>
<pre>var FLT_SIGNBIT = 0b10000000000000000000000000000000; // 2147483648
var FLT_EXPONENT = 0b01111111100000000000000000000000; // 2139095040
var FLT_MANTISSA = 0B00000000011111111111111111111111; // 8388607</code></pre>
<h3 id="八进制数字(Octal_numbers)">八进制数字(Octal numbers)</h3>
<p>八进制数字语法是以0为开头的。假如0后面的数字不在0到7的范围内该数字将会被转换成十进制数字。</p>
<pre>var n = 0755; // 493
var m = 0644; // 420
</code></pre>
<p>在ECMAScript 5 严格模式下禁止使用八进制语法。八进制语法并不是ECMAScript 5规范的一部分但是通过在八进制数字添加一个前缀0就可以被所有的浏览器支持0644 === 420 而且 "\045" === "%"。在ECMAScript 6中使用八进制数字是需要给一个数字添加前缀"0o"。</p>
<pre>var a = 0o10; // ES6 :八进制</code></pre>
<h3 id="十六进制(Hexadecimal_numbers)">十六进制(Hexadecimal numbers)</h3>
<p>十六进制数字语法是以零为开头后面接一个小写或大写的拉丁文字母X(<code>0x或者是0X</code>)。假如<code>0x</code>后面的数字超出规定范围(0123456789ABCDEF),那么就会提示这样的语法错误(<code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError">SyntaxError</a>)</code>"Identifier starts immediately after numeric literal".</p>
<pre>0xFFFFFFFFFFFFFFFFF // 295147905179352830000
0x123456789ABCDEF // 81985529216486900
0XA // 10
</code></pre>
<h3 id="指数形式(Exponentiation)">指数形式(Exponentiation)</h3>
<pre><code>1E3 // 1000
2e6 // 2000000
0.1e2 // 10</code></code></pre>
<h2 id="数字对象"><font face="Consolas, Liberation Mono, Courier, monospace"><code>数字对象</code></font></h2>
<p>内置的<a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Number</code></a>对象有一些有关数字的常量属性,如最大值、不是一个数字和无穷大的。你不能改变这些属性,但可以按下边的方式使用它们:</p>
<pre>var biggestNum = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var infiniteNum = Number.POSITIVE_INFINITY;
var negInfiniteNum = Number.NEGATIVE_INFINITY;
var notANum = Number.NaN;
</code></pre>
<p>你永远只用从Number对象引用上边显示的属性而不是你自己创建的Number对象的属性。</p>
<p>下面的表格汇总了数字对象的属性:</p>
<p><strong>数字的属性</strong></p>
<table>
<thead>
<tr>
<th scope="col">属性</th>
<th scope="col">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="Reference/Global_Objects/Number/MAX_VALUE" title="Number.MAX_VALUE 属性表示在 JavaScript 里所能表示的最大数值。"><code>Number.MAX_VALUE</code></a></td>
<td>可表示的最大值</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/MIN_VALUE" title="Number.MIN_VALUE 属性表示在 JavaScript 中所能表示的最小的正值。"><code>Number.MIN_VALUE</code></a></td>
<td>可表示的最小值</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/NaN" title="Number.NaN 表示“非数字”Not-A-Number。和 NaN 相同。"><code>Number.NaN</code></a></td>
<td>特指”非数字“</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/NEGATIVE_INFINITY" title="Number.NEGATIVE_INFINITY 属性表示负无穷大。"><code>Number.NEGATIVE_INFINITY</code></a></td>
<td>特指“负无穷”;在溢出时返回</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/POSITIVE_INFINITY" title="Number.POSITIVE_INFINITY 属性表示正无穷大。"><code>Number.POSITIVE_INFINITY</code></a></td>
<td>特指“正无穷”;在溢出时返回</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/EPSILON" title="Number.EPSILON 属性表示 1 与Number可表示的大于 1 的最小的浮点数之间的差值。"><code>Number.EPSILON</code></a></td>
<td>
<p>表示1和比最接近1且大于1的最小<a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Number</code></a>之间的差别</p>
</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/MIN_SAFE_INTEGER" title="Number.MIN_SAFE_INTEGER 代表在 JavaScript中最小的安全的integer型数字 (-(253 - 1))."><code>Number.MIN_SAFE_INTEGER</code></a></td>
<td>JavaScript最小安全整数.</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/MAX_SAFE_INTEGER" title="Number.MAX_SAFE_INTEGER 常量表示在 JavaScript 中最大的安全整数maxinum safe integer)253 - 1。"><code>Number.MAX_SAFE_INTEGER</code></a></td>
<td>JavaScript最大安全整数.</td>
</tr>
</tbody>
</table>
<table>
<caption>数字的方法</caption>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="Reference/Global_Objects/Number/parseFloat" title="Number.parseFloat() 方法可以把一个字符串解析成浮点数。该方法与全局的 parseFloat() 函数相同并且处于 ECMAScript 6 规范中(用于全局变量的模块化)。"><code>Number.parseFloat()</code></a></td>
<td>把字符串参数解析成浮点数,<br/>
和全局方法 <a href="Reference/Global_Objects/parseFloat" title="parseFloat() 函数解析一个字符串参数并返回一个浮点数。"><code>parseFloat()</code></a> 作用一致.</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/parseInt" title="Number.parseInt() 方法可以根据给定的进制数把一个字符串解析成整数。"><code>Number.parseInt()</code></a></td>
<td>
<p>把字符串解析成特定基数对应的整型数字,和全局方法 <a href="Reference/Global_Objects/parseInt" title="parseInt() 函数解析一个字符串参数,并返回一个指定基数的整数 (数学系统的基础)。"><code>parseInt()</code></a> 作用一致.</p>
</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/isFinite" title="Number.isFinite() 方法用来检测传入的参数是否是一个有穷数finite number。"><code>Number.isFinite()</code></a></td>
<td>判断传递的值是否为有限数字。</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/isInteger" title="Number.isInteger() 方法用来判断给定的参数是否为整数。"><code>Number.isInteger()</code></a></td>
<td>判断传递的值是否为整数。</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/isNaN" title="Number.isNaN() 方法确定传递的值是否为 NaN和其类型是 Number。它是原始的全局isNaN()的更强大的版本。"><code>Number.isNaN()</code></a></td>
<td>判断传递的值是否为 <a href="Reference/Global_Objects/NaN" title="全局属性 NaN 的值表示不是一个数字Not-A-Number。"><code>NaN</code></a>. More robust version of the original global <a href="Reference/Global_Objects/isNaN" title="isNaN() 函数用来确定一个值是否为NaN 。注isNaN函数内包含一些非常有趣的规则你也可以通过ECMAScript 2015/ES6 中定义的Number.isNaN()或者 可以使用typeof 来判断该值是否为一个非数字。"><code>isNaN()</code></a>.</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/isSafeInteger" title="Number.isSafeInteger() 方法用来判断传入的参数值是否是一个“安全整数”safe integer。一个安全整数是一个符合下面条件的整数"><code>Number.isSafeInteger()</code></a></td>
<td>判断传递的值是否为安全整数。</td>
</tr>
</tbody>
</table>
<p>数字的类型提供了不同格式的方法以从数字对象中检索信息。以下表格总结了 <code>数字类型原型上的方法。</code></p>
<table>
<caption>数字类型原型上的一些方法</caption>
<thead>
<tr>
<th scope="col">方法</th>
<th scope="col">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="Reference/Global_Objects/Number/toExponential" title="toExponential() 方法以指数表示法返回该数值字符串表示形式。"><code>toExponential()</code></a></td>
<td>返回一个数字的指数形式的字符串形如1.23e+2</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/toFixed" title="toFixed() 方法使用定点表示法来格式化一个数。"><code>toFixed()</code></a></td>
<td>
<p>返回指定小数位数的表示形式,</p>
<p>var a=123,b=a.toFixed(2)//b="123.00"</p>
</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/toPrecision" title="toPrecision() 方法以指定的精度返回该数值对象的字符串表示。"><code>toPrecision()</code></a></td>
<td>
<p>返回一个指定精度的数字。如下例子中a=123中3会由于精度限制消失</p>
<p>var a=123,b=a.toPrecision(2)//b="1.2e+2"</p>
</td>
</tr>
</tbody>
</table>
<h2 id="数学对象Math"><font face="consolas, Liberation Mono, courier, monospace">数学对象Math</font></h2>
<p>对于内置的<a href="Reference/Global_Objects/Math" title="Math 是一个内置对象 它具有数学常数和函数的属性和方法。不是一个函数对象。"><code>Math</code></a>数学常项和函数也有一些属性和方法。 比方说, <code>Math对象的</code> <code>PI</code> 属性会有属性值 pi (3.141...),你可以像这样调用它:</p>
<pre>Math.PI // π
</code></pre>
<p>同理标准数学函数也是Math的方法。 这些包括三角函数对数指数和其他函数。比方说你想使用三角函数 <code>sin</code>, 你可以这么写:</p>
<pre>Math.sin(1.56)
</code></pre>
<p>需要注意的是Math的所有三角函数参数都是弧度制。</p>
<p>下面的表格总结了 <code>Math</code> 对象的方法。</p>
<p>Math的方法</p>
<table>
<thead>
<tr>
<th scope="col">方法</th>
<th scope="col">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="Reference/Global_Objects/Math/abs" title="Math.abs(x) 函数返回指定数字 “x“ 的绝对值。如下:"><code>abs()</code></a></td>
<td>绝对值</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Math/sin" title="Math.sin() 函数返回一个数值的正弦值。"><code>sin()</code></a>, <a href="Reference/Global_Objects/Math/cos" title="Math.cos() 函数返回一个数值的余弦值。"><code>cos()</code></a>, <a href="Reference/Global_Objects/Math/tan" title="Math.tan() 方法返回一个数值的正切值。"><code>tan()</code></a></td>
<td>标准三角函数;参数为弧度</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Math/asin" title="Math.asin() 方法返回一个数值的反正弦(单位为弧度),即:"><code>asin()</code></a>, <a href="Reference/Global_Objects/Math/acos" title="Math.acos() 返回一个数的反余弦值(单位为弧度),即:"><code>acos()</code></a>, <a href="Reference/Global_Objects/Math/atan" title="Math.atan() 函数返回一个数值的反正切(以弧度为单位),即:"><code>atan()</code></a>, <a href="Reference/Global_Objects/Math/atan2" title="Math.atan2() 返回其参数比值的反正切值。"><code>atan2()</code></a></td>
<td>反三角函数; 返回值为弧度</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Math/sinh" title="Math.sinh() 函数返回一个数字(单位为角度)的双曲正弦值."><code>sinh()</code></a>, <a href="Reference/Global_Objects/Math/cosh" title="Math.cosh() 函数返回数值的双曲余弦函数, 可用 constant e 表示:"><code>cosh()</code></a>, <a href="Reference/Global_Objects/Math/tanh" title="Math.tanh() 函数将会返回一个数的双曲正切函数值,计算如下:"><code>tanh()</code></a></td>
<td>双曲三角函数; 返回值为弧度.</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Math/asinh" title="Math.asinh() 函数返回给定数字的反双曲正弦值, 即:"><code>asinh()</code></a>, <a class="new" href="Reference/Global_Objects/Math/acosh" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>acosh()</code></a>, <a href="Reference/Global_Objects/Math/atanh" title="Math.atanh() 函数返回一个数值反双曲正切值, 即:"><code>atanh()</code></a></td>
<td>反双曲三角函数;返回值为弧度.</td>
</tr>
<tr>
<td>
<p><a href="Reference/Global_Objects/Math/pow" title="Math.pow() 函数返回基数base的指数exponent次幂 baseexponent。"><code>pow()</code></a>, <a href="Reference/Global_Objects/Math/exp" title="Math.exp() 函数返回 exx 表示参数e 是欧拉常数Euler's constant自然对数的底数。"><code>exp()</code></a>, <a href="Reference/Global_Objects/Math/expm1" title="Math.expm1() 函数返回 Ex - 1, 其中 x 是该函数的参数, E 是自然对数的底数 2.718281828459045."><code>expm1()</code></a>, <a href="Reference/Global_Objects/Math/log10" title="Math.log10() 函数返回一个数字以 10 为底的对数."><code>log10()</code></a>, <a href="Reference/Global_Objects/Math/log1p" title="Math.log1p() 函数返回一个数字加1后的自然对数 (底为 E), 既log(x+1)."><code>log1p()</code></a>, <a href="Reference/Global_Objects/Math/log2" title="Math.log2() 函数返回一个数字以 2 为底的对数."><code>log2()</code></a></p>
</td>
<td>指数与对数函数</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Math/floor" title="Math.floor() 返回小于或等于一个给定数字的最大整数。"><code>floor()</code></a>, <a href="Reference/Global_Objects/Math/ceil" title="Math.ceil() 函数返回大于或等于一个给定数字的最小整数。"><code>ceil()</code></a></td>
<td>返回最大/最小整数小于/大于或等于参数</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Math/min" title="Math.min() 返回零个或更多个数值的最小值。"><code>min()</code></a>, <a href="Reference/Global_Objects/Math/max" title="Math.max() 函数返回一组数中的最大值。"><code>max()</code></a></td>
<td>
<p> </p>
<p>返回一个以逗号间隔的数字参数列表中的较小或较大值(分别地)</p>
</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Math/random" title="Math.random() 函数返回一个浮点,  伪随机数在范围[01)也就是说从0包括0往上但是不包括1排除1然后您可以缩放到所需的范围。实现将初始种子选择到随机数生成算法;它不能被用户选择或重置。"><code>random()</code></a></td>
<td>返回0和1之间的随机数。</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Math/round" title="Math.round() 函数返回一个数字四舍五入后最接近的整数。"><code>round()</code></a>, <a href="Reference/Global_Objects/Math/fround" title="Math.fround() 可以将任意的数字转换为离它最近的单精度浮点数形式的数字。"><code>fround()</code></a>, <a href="Reference/Global_Objects/Math/trunc" title="Math.trunc() 方法会将数字的小数部分去掉,只保留整数部分。"><code>trunc()</code></a>,</td>
<td>四舍五入和截断函数</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Math/sqrt" title="Math.sqrt() 函数返回一个数的平方根,即:"><code>sqrt()</code></a>, <a href="Reference/Global_Objects/Math/cbrt" title="Math.cbrt() 函数返回任意数字的立方根."><code>cbrt()</code></a>, <a href="Reference/Global_Objects/Math/hypot" title="Math.hypot() 函数返回它的所有参数的平方和的平方根,即:"><code>hypot()</code></a></td>
<td>
<p>平方根,立方根,平方参数的和的平方根 </p>
<p>两个参数平方和的平方根</p>
</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Math/sign" title="Math.sign() 函数返回一个数字的符号, 指示数字是正数,负数还是零。"><code>sign()</code></a></td>
<td>数字的符号, 说明数字是否为正、负、零。</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Math/clz32" title="Math.clz32() 函数返回一个数字在转换成 32 无符号整形数字的二进制形式后, 开头的 0 的个数, 比如 1000000 转换成 32 位无符号整形数字的二进制形式后是 00000000000011110100001001000000, 开头的 0 的个数是 12 个, 则 Math.clz32(1000000) 返回 12."><code>clz32()</code></a>,<br/>
<a href="Reference/Global_Objects/Math/imul" title="该函数返回两个参数的类C的32位整数乘法运算的运算结果."><code>imul()</code></a></td>
<td>
<p>在32位2进制表示中开头的0的数量.</p>
<p><em>返回传入的两个参数相乘结果的类C的32位表现形式</em></p>
</td>
</tr>
</tbody>
</table>
<p>和其他对象不同你不能够创建一个自己的Math对象。你只能使用内置的Math对象。</p>
<h2 id="日期对象"><font face="Consolas, Liberation Mono, Courier, monospace">日期对象</font></h2>
<p>JavaScript没有日期数据类型。但是你可以在你的程序里使用 <a href="Reference/Date" title="此页面仍未被本地化, 期待您的翻译!"><code>Date</code></a> 对象和其方法来处理日期和时间。Date对象有大量的设置、获取和操作日期的方法。 它并不含有任何属性。</p>
<p>JavaScript 处理日期数据类似于Java。这两种语言有许多一样的处理日期的方法也都是以1970年1月1日00:00:00以来的毫秒数来储存数据类型的。</p>
<p><code>Date</code> 对象的范围是相对距离 UTC 1970年1月1日 的前后 100,000,000 天。</p>
<p>创建一个日期对象:</p>
<pre>var dateObjectName = new Date([parameters]);
</code></pre>
<p>这里的 dateObjectName 对象是所创建的Date对象的一个名字它可以成为一个新的对象或者已存在的其他对象的一个属性。</p>
<p>不使用 <em>new </em>关键字来调用Date对象将返回当前时间和日期的字符串</p>
<p>前边的语法中的参数parameters可以是一下任何一种</p>
<ul>
<li>无参数 : 创建今天的日期和时间,例如: <code>today = new Date();</code>.</li>
<li>一个符合以下格式的表示日期的字符串: "月 日, 年 时:分:秒." 例如: <code>var Xmas95 = new Date("December 25, 1995 13:30:00")。</code>如果你省略时、分、秒那么他们的值将被设置为0。</li>
<li>一个年,月,日的整型值的集合,例如: <code>var Xmas95 = new Date(1995, 11, 25)。</code></li>
<li>一个年,月,日,时,分,秒的集合,例如: <code>var Xmas95 = new Date(1995, 11, 25, 9, 30, 0);</code>.</li>
</ul>
<h3 id="Date对象的方法"><code>Date对象的方法</code></h3>
<p>处理日期时间的Date对象方法可分为以下几类</p>
<ul>
<li>"set" 方法, 用于设置Date对象的日期和时间的值。</li>
<li>"get" 方法,用于获取Date对象的日期和时间的值。</li>
<li>"to" 方法,用于返回Date对象的字符串格式的值。</li>
<li>parse 和UTC 方法, 用于解析Date字符串。</li>
</ul>
<p>通过“get”和“set”方法你可以分别设置和获取秒星期月份年。这里有个getDay方法可以返回星期但是没有相应的setDay方法用来设置星期因为星期是自动设置的。这些方法用整数来代表以下这些值</p>
<ul>
<li> 0 至 59</li>
<li> 0 至 23</li>
<li>星期 0 (周日) 至 6 (周六)</li>
<li>日期1 至 31 </li>
<li>月份 0 (一月) to 11 (十二月)</li>
<li>年份 从1900开始的年数</li>
</ul>
<p>例如, 假设你定义了如下日期:</p>
<pre>var Xmas95 = new Date("December 25, 1995");
</code></pre>
<p>Then <code>Xmas95.getMonth()</code> 返回 11, and <code>Xmas95.getFullYear()</code> 返回 1995.</p>
<p><code>getTime</code> 和 <code>setTime</code> 方法对于比较日期是非常有用的。<code>getTime</code>方法返回从1970年1月1日00:00:00的毫秒数。</p>
<p>例如,以下代码展示了今年剩下的天数:</p>
<pre><code>var today = new Date();
var endYear = new Date(1995, 11, 31, 23, 59, 59, 999); // 设置日和月注意月份是0-11
endYear.setFullYear(today.getFullYear()); // 把年设置为今年
var msPerDay = 24 * 60 * 60 * 1000; // 每天的毫秒数
var daysLeft = (endYear.getTime() - today.getTime()) / msPerDay;
var daysLeft = Math.round(daysLeft); //返回今年剩下的天数</code>
</code></pre>
<p>这个例子中,创建了一个包含今天的日期的<code>Date</code>对象,并命名为<code>today</code>,然后创建了一个名为<code>endYear</code><code>Date</code>对象,并把年份设置为当前年份,接着使用<code>today</code><code>endYear</code><code>getTime</code>分别获取今天和年底的毫秒数,再根据每一天的毫秒数,计算出了今天到年底的天数,最后四舍五入得到今年剩下的天数。</p>
<p>parse方法对于从日期字符串赋值给现有的Date对象很有用例如以下代码使用<code>parse</code><code>setTime</code>分配了一个日期值给<code>IPOdate</code>对象:</p>
<pre>var IPOdate = new Date();
IPOdate.setTime(Date.parse("Aug 9, 1995"));
</code></pre>
<h3 id="例子:">例子:</h3>
<p>在下边的例子中JSClock()函数返回了用数字时钟格式的时间:</p>
<pre>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;
}
</code></pre>
<p><code>JSClock函数首先创建了一个叫做time的新的Date对象因为没有参数所以time代表了当前日期和时间。然后调用了</code><code>getHours</code>, <code>getMinutes以及getSeconds方法把当前的时分秒分别赋值给了hour</code>, <code>minute</code>,<code>second。</code></p>
<p>接下来的4句在time的基础上创建了一个字符串第一句创建了一个变量temp并通过一个条件表达式进行了赋值如果小时大于12就为 (<code>hour - 12</code>), 其他情况就为 hour, 除非 hour 为 0, 这种情况下,它会变成 12.</p>
<p>接下来的语句拼接了<code>minute</code>的值到<code>temp后。如果minute小于10条件表达式就会在minute前边加个0其他情况下加一个分号。然后按同样的方式在temp后拼接上了秒。</code></p>
<p>最后如果hour是12或者更大条件表达式会在temp后拼接"P.M.",否则拼接"A.M." 。</p>
<p></p><div class="prevnext" style="text-align: right;">
<p><a href="Guide/Expressions_and_Operators" style="float: left;">« 上一页</a><a href="Guide/Text_formatting">下一页 »</a></p>
</div><p></p>
</article>