function aaa() { var s = "2010-5-6"; var a = s.split("-"); if (a.length > 2) { var y = a[0]; var m = a[1]; var d = a[2]; alert(y + " " + m + " " + d); } }
有没有直接一点的办法呢,如 myDate.getYear(); ...myDate.getDate();但我不知道应该怎么将字符串 '2010-5-6' 或 '2010-5-6 13:22:4' 这类的时间字符串转换为 Date 类型
function aaa() { var d = new Date('2010-5-6 13:22:4'); alert(d.getFullYear() + " " + (d.getMonth()+1) + " " + d.getDate()); }
//+--------------------------------------------------- //| 取得日期数据信息 //| 参数 interval 表示数据类型 //| y 年 m月 d日 w 星期 ww周 h时 n分 s秒 //+--------------------------------------------------- Date.prototype.DatePart = function(interval) { var myDate = this; var partStr = ''; var Week = ['日', '一', '二', '三', '四', '五', '六']; switch (interval.toLowerCase()) { case 'y': partStr = myDate.getFullYear(); break; case 'm': partStr = myDate.getMonth() + 1; break; case 'd': partStr = myDate.getDate(); break; case 'w': partStr = Week[myDate.getDay()]; break; case 'ww': partStr = myDate.WeekNumOfYear(); break; case 'h': partStr = myDate.getHours(); break; case 'n': partStr = myDate.getMinutes(); break; case 's': partStr = myDate.getSeconds(); break; } return partStr; }
不对啊,这样返回的都是 NaN NaN NaN
我如果将 '2010-5-6' 或 '2010-5-6 13:22:4' 这类的时间字符串 转换成 myDate ,直接这么转var d = new Date('2010-5-6 13:22:4');的话 d 是 NaN
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script> function aaa() { var d = new Date('2010-5-6 13:22:4'); alert(d); alert(d.getFullYear() + " " + (d.getMonth() + 1) + " " + d.getDate()); return false; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return aaa()" /> </div> </form> </body> </html>
Date 对象会自动把当前日期和时间保存为其初始值。 参数形式有以下5种: new Date("month dd,yyyy hh:mm:ss"); new Date("month dd,yyyy"); new Date(yyyy,mth,dd,hh,mm,ss); new Date(yyyy,mth,dd); new Date(ms);还是切出来了. 用下面的方法格式化. new Date(2010, 5, 6, 13, 22, 4);//ie下面无效. chrome下面测试没问题. new Date('2010-5-6 13:22:4');
参数形式有以下5种: new Date("month dd,yyyy hh:mm:ss"); new Date("month dd,yyyy"); new Date(yyyy,mth,dd,hh,mm,ss); new Date(yyyy,mth,dd); new Date(ms);但这里有一个要注意的问题。是大家都会忽略的。就是 js的月取出来要+1 因此在new 的时候别忘了-1. 本人遇到过。两个时间先减天数成负值了。就是因为这个问题。 例如:要格式化 2010-9-10 一定是:new Date(2010,8,10);
function aaa() {
var s = "2010-5-6";
var a = s.split("-");
if (a.length > 2) {
var y = a[0];
var m = a[1];
var d = a[2];
alert(y + " " + m + " " + d);
}
}
function aaa() {
var d = new Date('2010-5-6 13:22:4');
alert(d.getFullYear() + " " + (d.getMonth()+1) + " " + d.getDate());
}
//| 取得日期数据信息
//| 参数 interval 表示数据类型
//| y 年 m月 d日 w 星期 ww周 h时 n分 s秒
//+---------------------------------------------------
Date.prototype.DatePart = function(interval) {
var myDate = this;
var partStr = '';
var Week = ['日', '一', '二', '三', '四', '五', '六'];
switch (interval.toLowerCase()) {
case 'y': partStr = myDate.getFullYear(); break;
case 'm': partStr = myDate.getMonth() + 1; break;
case 'd': partStr = myDate.getDate(); break;
case 'w': partStr = Week[myDate.getDay()]; break;
case 'ww': partStr = myDate.WeekNumOfYear(); break;
case 'h': partStr = myDate.getHours(); break;
case 'n': partStr = myDate.getMinutes(); break;
case 's': partStr = myDate.getSeconds(); break;
}
return partStr;
}
我如果将 '2010-5-6' 或 '2010-5-6 13:22:4' 这类的时间字符串 转换成 myDate ,直接这么转var d = new Date('2010-5-6 13:22:4');的话 d 是 NaN
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function aaa() {
var d = new Date('2010-5-6 13:22:4');
alert(d);
alert(d.getFullYear() + " " + (d.getMonth() + 1) + " " + d.getDate());
return false;
} </script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return aaa()" />
</div>
</form>
</body>
</html>
参数形式有以下5种:
new Date("month dd,yyyy hh:mm:ss");
new Date("month dd,yyyy");
new Date(yyyy,mth,dd,hh,mm,ss);
new Date(yyyy,mth,dd);
new Date(ms);还是切出来了. 用下面的方法格式化.
new Date(2010, 5, 6, 13, 22, 4);//ie下面无效. chrome下面测试没问题.
new Date('2010-5-6 13:22:4');
new Date("month dd,yyyy hh:mm:ss");
new Date("month dd,yyyy");
new Date(yyyy,mth,dd,hh,mm,ss);
new Date(yyyy,mth,dd);
new Date(ms);但这里有一个要注意的问题。是大家都会忽略的。就是 js的月取出来要+1
因此在new 的时候别忘了-1.
本人遇到过。两个时间先减天数成负值了。就是因为这个问题。
例如:要格式化 2010-9-10 一定是:new Date(2010,8,10);