在javascript中直接输出Date得到的结果是这样的:
function date(){
<wbr><wbr><wbr><wbr>var date = new Date();</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>alert(date);</wbr></wbr></wbr>
}
结果是:Mon Jun 15 15:30:46 UTC+0800 2009
这可能不是我们所需要的,因此是需要转换下的,这里就学下我转换的几种方法,不妥之处请指教:
<wbr></wbr>
1、得到new Date()型中各个时间级别(年、月、日、时、分、秒)的数:
function date(){
<wbr><wbr><wbr><wbr>var date = new Date();</wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr><wbr>var year = date.getFullYear();<br><wbr><wbr><wbr><wbr>var month = date.getMonth()+1;<wbr><wbr><wbr>//js从0开始取<wbr><br><wbr><wbr><wbr><wbr>var date1 = date.getDate();<wbr><br><wbr><wbr><wbr><wbr>var hour = date.getHours();<wbr><br><wbr><wbr><wbr><wbr>var minutes = date.getMinutes();<wbr><br><wbr><wbr><wbr><wbr>var second = date.getSeconds();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr><wbr>alert(date+"<wbr><wbr>|<wbr>"+year+"年"+month+"月"+date1+"日"+hour+"时"+minutes<wbr>+"分"+second+"秒" );</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
}
得到的结果就是:Mon Jun 15 15:44:50 UTC+0800 2009 | 2009年6月15日15时44分50秒
<wbr></wbr>
注意:date.getMonth()得到的月是从0开始的,date所有的get方法除了date.getFullYear()得到的是2009外,其余的get方法得到的数若小于10,都是以单数出现,如6月,date.getMonth()+1 = 6 若要得到两位的自己加上0就可以了,如下面的转换方法。
<wbr></wbr>
以下是转换成另两种格式的,和上面是大同小异的:
(1)
//将date型转换为tring<wbr></wbr>
//传来的datetime是:Wed Mar 04 2009 11:05:05 GMT+0800格式<wbr>得到结果:2009-06-12 17:18:05</wbr>
function dateToStr(datetime){<wbr></wbr>
<wbr>var year = datetime.getFullYear();<br><wbr>var month = datetime.getMonth()+1;//<span style="color:rgb(255,0,0)">js从0开始取<wbr><br></wbr></span><wbr>var date = datetime.getDate();<wbr><br><wbr>var hour = datetime.getHours();<wbr><br><wbr>var minutes = datetime.getMinutes();<wbr><br><wbr>var second = datetime.getSeconds();<br><wbr><br><wbr>if(month<10){<br><wbr><wbr>month = "0" + month;<br><wbr>}<br><wbr>if(date<10){<br><wbr><wbr>date = "0" + date;<br><wbr>}<br><wbr>if(hour <10){<br><wbr><wbr>hour = "0" + hour;<br><wbr>}<br><wbr>if(minutes <10){<br><wbr><wbr>minutes = "0" + minutes;<br><wbr>}<br><wbr>if(second <10){<br><wbr><wbr>second = "0" + second ;<br><wbr>}<br><wbr><br><wbr>var time = year+"-"+month+"-"+date+" "+hour+":"+minutes+":"+second; //2009-06-12 17:18:05<br> //<wbr>alert(time);<br><wbr>return time;<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr>
<wbr></wbr>
(2)
//将date型转换为tring<wbr></wbr>
//传来的datetime是:Wed Mar 04 2009 11:05:05 GMT+0800格式得到结果:09年06月12日 17时18分
function dateToComm(datetime){<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr>var year = datetime.getFullYear();<br><wbr>var month = datetime.getMonth()+1;//<span style="color:rgb(255,0,0)">js从0开始取<wbr><br></wbr></span><wbr>var date = datetime.getDate();<wbr><br><wbr>var hour = datetime.getHours();<wbr><br><wbr>var minutes = datetime.getMinutes();<wbr><br><wbr>var second = datetime.getSeconds();<br><wbr><br><wbr>if(month<10){<br><wbr><wbr>month = "0" + month;<br><wbr>}<br><wbr>if(date<10){<br><wbr><wbr>date = "0" + date;<br><wbr>}<br><wbr>if(hour <10){<br><wbr><wbr>hour = "0" + hour;<br><wbr>}<br><wbr>if(minutes <10){<br><wbr><wbr>minutes = "0" + minutes;<br><wbr>}<br><wbr>if(second <10){<br><wbr><wbr>second = "0" + second ;<br><wbr>}<br><wbr><br><wbr>year = year.toString();<br><wbr>year = year.substring(2);<br><wbr>var time1 = year+"年"+month+"月"+date+"日";<br><wbr>var time2 = hour+"时"+minutes+"分";//09年06月12日 17时18分<br><wbr>var time={time1:time1,time2:time2}<wbr><wbr><wbr>//<span style="color:rgb(255,0,0)">json格式</span><br> //<wbr>alert(time);<br><wbr>return time;<wbr><br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr>
2、实际应用中经常会遇到加减时间的情况,如:昨天这个时间到今天这个时间间系统共登陆了几次之类的。因此在我们取得当前时间之后就要减去一天或几天。
在此我们现将得到的时间转换下:
function date(){
<wbr><wbr>var date = new Date();</wbr></wbr>
<wbr><wbr><wbr>var dateNum date.getTime();</wbr></wbr></wbr>
<wbr><wbr>alert(dateNum);</wbr></wbr>
}
得到的是:2592000000<wbr><wbr><wbr><wbr>这个是距 1970 年 1 月 1 日之间的毫秒数。</wbr></wbr></wbr></wbr>
注:getTime() 方法可返回距 1970 年 1 月 1 日之间的毫秒数。
<wbr></wbr>
我们以一天为例,得到一天前的此时:
function date(){
<wbr><wbr>var date = new Date();</wbr></wbr>
<wbr><wbr>var oneDate = 1</wbr></wbr>
<wbr><wbr>var dateMid = date.getTime() - (oneDate * 24 * 60 * 60 * 1000)</wbr></wbr>
<wbr><wbr>var dateBefore = new Date(dateMid);</wbr></wbr>
<wbr><wbr>alert(dateBefore);</wbr></wbr>
}
若date 为:Mon Jun 15 15:44:50 UTC+0800 2009<wbr><wbr></wbr></wbr>
则输出:Mon Jun 14 15:44:50 UTC+0800 2009<wbr><wbr></wbr></wbr>
要转换为Sreing型<wbr>按照上面1的方法转换就可以了。</wbr>