JavaScript中日期函數(shù)的相關(guān)操作知識(shí)
時(shí)間對(duì)象是一個(gè)我們經(jīng)常要用到的對(duì)象,無論是做時(shí)間輸出、時(shí)間判斷等操作時(shí)都與這個(gè)對(duì)象離不開。除開JavaScript中的時(shí)間對(duì)象外,在VbScript中也有許多的時(shí)間對(duì)象,而且非常好用。下面還是按照我們的流程來進(jìn)行講解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)
下面對(duì)
1.new Date(),沒有參數(shù)的時(shí)候,創(chuàng)建的是當(dāng)前時(shí)間日期對(duì)象。
2.new Date(milliseconds),當(dāng)參數(shù)為數(shù)字的時(shí)候,那么這個(gè)參數(shù)就是時(shí)間戳,被視為毫秒,創(chuàng)建一個(gè)距離1970年1月一日指定毫秒的時(shí)間日期對(duì)象。
3.new Date(datestring),此參數(shù)是一個(gè)字符串,并且此字符串一定能夠使用Date.parse()轉(zhuǎn)換。
4.以下六個(gè)構(gòu)造函數(shù)是精確定義:
1).year,是一個(gè)整數(shù),如果是0-99,那么在此基礎(chǔ)上加1900,其他的都原樣返回。
2).month,是一個(gè)整數(shù),范圍是0-11。
3).day,是一個(gè)整數(shù),范圍是1-31。
4).hours,是一個(gè)整數(shù),范圍是0-23。
5).minutes,是一個(gè)整數(shù),范圍是0-59。
6).seconds,是一個(gè)整數(shù),范圍是0-59。
7).microseconds 是一個(gè)整數(shù),范圍是0-9999。
<html>
<head>
<title>時(shí)間戳轉(zhuǎn)化為年月日時(shí)分秒</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)時(shí)間
var shijianchuo = now.getTime();//獲取當(dāng)前時(shí)間戳
alert("時(shí)間戳:"+shijianchuo);
var nowdate = new Date(shijianchuo);//將時(shí)間戳轉(zhuǎn)化為日期對(duì)象
var nowtime=nowdate.Format("yyyy-MM-dd hh:mm:ss");//格式化當(dāng)前系統(tǒng)時(shí)間,相當(dāng)于將時(shí)間戳轉(zhuǎn)化為年月日時(shí)分秒了
alert("當(dāng)前時(shí)間:"+nowtime);
}
/*
日期格式化:
對(duì)Date的擴(kuò)展,將 Date 轉(zhuǎn)化為指定格式的String
年(y)可以用1-4個(gè)占位符,季度(q)可以用1-2個(gè)占位符.
月(M)、日(d)、小時(shí)(h)、分(m)、秒(s)可以用1-2個(gè)占位符.
毫秒(S)只能用1個(gè)占位符(是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(), //時(shí)
"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)文章
JavaScript對(duì)象屬性操作實(shí)例解析
這篇文章主要介紹了JavaScript對(duì)象屬性操作實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
css值轉(zhuǎn)換成數(shù)值請(qǐng)拋棄parseInt
絕大多數(shù)人喜歡用parseInt()把css中的字符串值轉(zhuǎn)換成數(shù)值2011-10-10
基于javascript實(shí)現(xiàn)樣式清新圖片輪播特效
這篇文章主要為大家詳細(xì)介紹了基于javascript實(shí)現(xiàn)樣式清新圖片輪播特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03

