JavaScript中日期函數(shù)的相關(guān)操作知識
時間對象是一個我們經(jīng)常要用到的對象,無論是做時間輸出、時間判斷等操作時都與這個對象離不開。除開JavaScript中的時間對象外,在VbScript中也有許多的時間對象,而且非常好用。下面還是按照我們的流程來進行講解JavaScript中日期函數(shù)。
new Date()
new Date(milliseconds)
new Date(datestring)
new Date(year, month)
new Date(year, month, day)
new Date(year, month, day, hours)
new Date(year, month, day, hours, minutes)
new Date(year, month, day, hours, minutes, seconds)
new Date(year, month, day, hours, minutes, seconds, microseconds)
下面對
1.new Date(),沒有參數(shù)的時候,創(chuàng)建的是當(dāng)前時間日期對象。
2.new Date(milliseconds),當(dāng)參數(shù)為數(shù)字的時候,那么這個參數(shù)就是時間戳,被視為毫秒,創(chuàng)建一個距離1970年1月一日指定毫秒的時間日期對象。
3.new Date(datestring),此參數(shù)是一個字符串,并且此字符串一定能夠使用Date.parse()轉(zhuǎn)換。
4.以下六個構(gòu)造函數(shù)是精確定義:
1).year,是一個整數(shù),如果是0-99,那么在此基礎(chǔ)上加1900,其他的都原樣返回。
2).month,是一個整數(shù),范圍是0-11。
3).day,是一個整數(shù),范圍是1-31。
4).hours,是一個整數(shù),范圍是0-23。
5).minutes,是一個整數(shù),范圍是0-59。
6).seconds,是一個整數(shù),范圍是0-59。
7).microseconds 是一個整數(shù),范圍是0-9999。
<html>
<head>
<title>時間戳轉(zhuǎn)化為年月日時分秒</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
</body>
</html>
<script>
window.onload=function(){
var now=new Date();//當(dāng)前系統(tǒng)時間
var shijianchuo = now.getTime();//獲取當(dāng)前時間戳
alert("時間戳:"+shijianchuo);
var nowdate = new Date(shijianchuo);//將時間戳轉(zhuǎn)化為日期對象
var nowtime=nowdate.Format("yyyy-MM-dd hh:mm:ss");//格式化當(dāng)前系統(tǒng)時間,相當(dāng)于將時間戳轉(zhuǎn)化為年月日時分秒了
alert("當(dāng)前時間:"+nowtime);
}
/*
日期格式化:
對Date的擴展,將 Date 轉(zhuǎn)化為指定格式的String
年(y)可以用1-4個占位符,季度(q)可以用1-2個占位符.
月(M)、日(d)、小時(h)、分(m)、秒(s)可以用1-2個占位符.
毫秒(S)只能用1個占位符(是1-3位的數(shù)字)
例子:
(new Date()).Format("yyyy-MM-dd hh:mm:ss.S")
(new Date()).Format("yyyy-MM-dd hh:mm:ss.S毫秒 第qq季度")
*/
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月
"d+": this.getDate(), //日
"h+": this.getHours(), //時
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ?
(o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
</script>
相關(guān)文章
css值轉(zhuǎn)換成數(shù)值請拋棄parseInt
絕大多數(shù)人喜歡用parseInt()把css中的字符串值轉(zhuǎn)換成數(shù)值2011-10-10
基于javascript實現(xiàn)樣式清新圖片輪播特效
這篇文章主要為大家詳細介紹了基于javascript實現(xiàn)樣式清新圖片輪播特效,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03

