<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>南芝 - 我才12岁（飞鸟尽，良弓藏，2011，背包去旅行！） &#187; Date()</title>
	<atom:link href="http://www.12sui.cn/tag/date/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.12sui.cn</link>
	<description>南芝的前端技术学习经验</description>
	<lastBuildDate>Sun, 08 Jan 2012 09:22:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>new Date()</title>
		<link>http://www.12sui.cn/develop/new-date-2/</link>
		<comments>http://www.12sui.cn/develop/new-date-2/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 12:56:13 +0000</pubDate>
		<dc:creator>南芝</dc:creator>
				<category><![CDATA[编码]]></category>
		<category><![CDATA[Date()]]></category>

		<guid isPermaLink="false">http://www.12sui.cn/?p=773</guid>
		<description><![CDATA[昨天在调试bug的时候遇到一个问题，先看下代码： &#60;script&#62; var str = '2010-01-20'; alert(new Date(str.split('-').join(','))); &#60;/script&#62; 大家觉得这个会弹出来什么时间呢？2010年1月20日 or 2009年12月20日？。。。答案是丰富多彩的，看下在各浏览器下的情况： Firefox、Chrome 输出了2010年1月20日，Opera 输出了 2010年1月1日，Safari 提示非法时间，IE 输出了 NaN，这是为什么呢？ 查阅权威指南ing&#8230;. new Date()可以有三种类型的参数，一是数字（milliseconds），表示毫秒，一是字符串（datestring），其格式是Date.parse()接受的格式，一是年月日&#8230;.（year,month,day,hours,minutes,seconds,ms），我这里显然用的是第二种字符串了，ie 输出 NaN 表示ie其实并不支持这第二种参数形式，大家不妨自测一下，其它浏览器有正确输出的也有提示格式不对的等等，那么原因肯定在于各浏览器所能接受的格式有所不同，让我们进一步进行测试，测试结果如下。 其实我那么些的目的是为了使用第三种形式的参数：2010,1,20，结果。。。被我忽略的是其实我传进去的是一个字符串。。。也就是使用了第二种参数形式。。。杯具呀。。。 各浏览器支持的字符串的时间分隔符表： 分隔符IEOperaFirefoxChromeSafari /√√√√√ -√√×√× ,××√√× #×××√× *×××√× $×××√× !×××√× @×××√× %×××√× .×××√× ?×××√× &#8216;×××√× &#8220;×××√× ^××××× ~××××× (,),[,],{,}××××× +××××× :××××× \××××× &#124;××××× 感叹一下，Chrome 好变态，基本上所有符号都支持了。。。或许有一天我们可以用它来区分各浏览器哈。。。另外结果页证明了我前边的推断ie不支持第二种形式的参数时错误的，ie支持 / 这个分隔符的，另外也支持 &#8220;Aug 9, 1995&#8243; [...]]]></description>
			<content:encoded><![CDATA[<p>昨天在调试bug的时候遇到一个问题，先看下代码：</p>

<p><pre>
&lt;script&gt;
var str = '2010-01-20';
alert(new Date(str.split('-').join(',')));
&lt;/script&gt;
</pre></p>

<p>大家觉得这个会弹出来什么时间呢？2010年1月20日 or 2009年12月20日？。。。答案是丰富多彩的，看下在各浏览器下的情况：</p>

<p><a href="http://www.12sui.cn/wp-content/uploads/2010/01/new-date.png"><img src="http://www.12sui.cn/wp-content/uploads/2010/01/new-date-463x1024.png" alt="new date" title="new date" width="463" height="1024" /></a></p>

<p><span id="more-773"></span></p>

<p>Firefox、Chrome 输出了2010年1月20日，Opera 输出了 2010年1月1日，Safari 提示非法时间，IE 输出了 NaN，这是为什么呢？</p>

<p>查阅权威指南ing&#8230;.</p>

<p>new Date()可以有三种类型的参数，一是数字（milliseconds），表示毫秒，一是字符串（datestring），其格式是Date.parse()接受的格式，一是年月日&#8230;.（year,month,day,hours,minutes,seconds,ms），我这里显然用的是第二种字符串了，ie 输出 NaN 表示ie其实并不支持这第二种参数形式，大家不妨自测一下，其它浏览器有正确输出的也有提示格式不对的等等，那么原因肯定在于各浏览器所能接受的格式有所不同，让我们进一步进行测试，测试结果如下。</p>

<p style="color:red">其实我那么些的目的是为了使用第三种形式的参数：2010,1,20，结果。。。被我忽略的是其实我传进去的是一个字符串。。。也就是使用了第二种参数形式。。。杯具呀。。。</p>

<p><strong>各浏览器支持的字符串的时间分隔符表：</strong></p>

<table class="compatibility-table">
<thead>
<tr><th>分隔符</th><th>IE</th><th>Opera</th><th>Firefox</th><th>Chrome</th><th>Safari</th></tr>
</thead>
<tbody>
<tr>
<td>/</td><td class="ok">√</td><td class="ok">√</td><td class="ok">√</td><td class="ok">√</td><td class="ok">√</td>
</tr>
<tr>
<td>-</td><td class="ok">√</td><td class="ok">√</td><td class="no">×</td><td class="ok">√</td><td class="no">×</td>
</tr>
<tr>
<td>,</td><td class="no">×</td><td class="no">×</td><td class="ok">√</td><td class="ok">√</td><td class="no">×</td>
</tr>
<tr>
<td>#</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="ok">√</td><td class="no">×</td>
</tr>
<tr>
<td>*</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="ok">√</td><td class="no">×</td>
</tr>
<tr>
<td>$</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="ok">√</td><td class="no">×</td>
</tr>
<tr>
<td>!</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="ok">√</td><td class="no">×</td>
</tr>
<tr>
<td>@</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="ok">√</td><td class="no">×</td>
</tr>
<tr>
<td>%</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="ok">√</td><td class="no">×</td>
</tr>
<tr>
<td>.</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="ok">√</td><td class="no">×</td>
</tr>
<tr>
<td>?</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="ok">√</td><td class="no">×</td>
</tr>
<tr>
<td>&#8216;</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="ok">√</td><td class="no">×</td>
</tr>
<tr>
<td>&#8220;</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="ok">√</td><td class="no">×</td>
</tr>
<tr>
<td>^</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="no">×</td>
</tr>
<tr>
<td>~</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="no">×</td>
</tr>
<tr>
<td>(,),[,],{,}</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="no">×</td>
</tr>
<tr>
<td>+</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="no">×</td>
</tr>
<tr>
<td>:</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="no">×</td>
</tr>
<tr>
<td>\</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="no">×</td>
</tr>
<tr>
<td>|</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="no">×</td><td class="no">×</td>
</tr>
</tbody>
</table>

<p>感叹一下，Chrome 好变态，基本上所有符号都支持了。。。或许有一天我们可以用它来区分各浏览器哈。。。<span style="color:red;font-weight:600">另外结果页证明了我前边的推断ie不支持第二种形式的参数时错误的，ie支持 / 这个分隔符的，另外也支持 &#8220;Aug 9, 1995&#8243; 之类的标准格式。</span></p>

<p>这个 bug 解决的方法其实很简单，就是采用第三种参数形式或者使用 &#8216;/&#8217; 来分隔：</p>

<p><pre>
&lt;script&gt;
var arr = '2010-01-20'.split('-');
alert(new Date(arr[0],arr[1]-1,arr[2]));</p>

<p>//or
var arr2 = '2010-01-20'.split('-');
alert(new Date(arr2.join('/'));
&lt;/script&gt;
</pre></p>

<p>看Firefox的 change log，应该是3.6开始支持 &#8211; 的，但是我这里测试没通过？记得在公司测试的时候是通过的呀。。。大家帮我测试一下吧。。。</p>

<p><strong>另外，在看权威指南的时候，又看到一句：</strong></p>

<p><q cite="《权威指南第五版》">Date()还可以作为普通函数被调用，而不带有运算符new。以这种方式调用时，Date()将忽略传递给它的所有参数，返回当前日期和事件的字符串标识。</q></p>

<p>^_^可以省几个字符了&#8230;</p>

<p>写了一个简单的脚本测试浏览器的支持情况，大家可以试下哈：<a href="http://www.12sui.cn/test/new-date-test.html">http://www.12sui.cn/test/new-date-test.html</a></p>

<p style="color:red">注：这里测试的仅是连接符类型的，其它的诸如&#8221;Aug 9, 1995&#8243;这种形式的没有做测试。测试浏览器为：IE6、IE7、IE8、Opera 10.10(Build 1893)、Firefox 3.6、Chrome 4.0.302.2、Safari 4.0.3(531.9.1)。</p>

<p><strong>参考资料：</strong></p>

<ul>
<li>JavaScript权威指南第五版</li>
<li><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/parse">Mozilla</a><li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.12sui.cn/develop/new-date-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>new Date()</title>
		<link>http://www.12sui.cn/develop/new-date/</link>
		<comments>http://www.12sui.cn/develop/new-date/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 23:59:11 +0000</pubDate>
		<dc:creator>南芝</dc:creator>
				<category><![CDATA[编码]]></category>
		<category><![CDATA[Date()]]></category>

		<guid isPermaLink="false">http://www.12sui.cn/?p=519</guid>
		<description><![CDATA[土鳖的记录下，昨天在计算昨天的时间时试着使用了new Date(new Date()*1 &#8211; 24*3600*1000)竟然可以正确的算出，当时惊讶了下哈，查了JavaScript权威指南，它的参数可以是数字（毫秒）、字符串（同Date.parse()的参数，格式必须是月日年这样子）以及（年、月、日&#8230;）等格式~ 另外，无比兴奋的的是Opera 10.10 Build1840支持了Opera Widgets的导出，使其不依赖Opera运行，虽然我的Opera都是一直开着的，但是我还是很高兴Opera这一伟大决定，希望Opera Widgets成倍的增长吧，Opera 10.10 Build1840下载。]]></description>
			<content:encoded><![CDATA[<p>土鳖的记录下，昨天在计算昨天的时间时试着使用了new Date(new Date()*1 &#8211; 24*3600*1000)竟然可以正确的算出，当时惊讶了下哈，查了JavaScript权威指南，它的参数可以是数字（毫秒）、字符串（同Date.parse()的参数，格式必须是月日年这样子）以及（年、月、日&#8230;）等格式~</p>

<p>另外，无比兴奋的的是Opera 10.10 Build1840支持了Opera Widgets的导出，使其不依赖Opera运行，虽然我的Opera都是一直开着的，但是我还是很高兴Opera这一伟大决定，希望Opera Widgets成倍的增长吧，<a href="http://www.csser.name/viewnews-1535">Opera 10.10 Build1840下载</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.12sui.cn/develop/new-date/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

