標籤:
1.Date日期
--Date 對象用於處理日期和時間。
Date 對象的文法:
var myDate=new Date()
2.Date 對象方法
eg:
<style type="text/css">
#date{
width: 400px;
height: 30px;
}
</style>
<body>
<input type="text" id="date">
<input type="button" value="當前日期" id="btn">
<input type="button" value="獲得年月日時分秒" id="btn1">
</body>
<script type="text/javascript">
var date=document.getElementById("date");
var btn=document.getElementById("btn");
var btn1=document.getElementById("btn1");
btn.onclick=function(){
date.value= Date();//返回當前日期
};
btn1.onclick=function(){
var nDat=new Date();
date.value=nDat.getDate();//當前月份的某一天,1--31的整數
date.value=nDat.getMonth();//月份,1--11的整數
date.value=nDat.getFullYear();//四位元年份
date.value=nDat.getHours();//小時
date.value=nDat.getMinutes();//分鐘
date.value=nDat.getSeconds();//秒
date.value=nDat.getMilliseconds();//毫秒
date.value=nDat.getMonth()+1;//月份,1--11的整數
var b=date.value;
date.value="現在時刻:"+
nDat.getFullYear()+"年"+b+"月"+nDat.getDate()+"日"+
nDat.getHours()+"點"+nDat.getMinutes()+"分" +nDat.getSeconds()+"秒"+
nDat.getMilliseconds();
};
var nDat=new Date();
var seDat= "現在時刻:"+
nDat.setFullYear(1970)+"年"+nDat.setMonth(1)+"月"+nDat.setDate(1)+"日"+
nDat.setHours(12)+"點"+nDat.setMinutes(12)+"分" +nDat.setSeconds(12)+"秒"+
nDat.setMilliseconds(12);
document.write(nDat);
</script>
3.Math 對象
--Math 對象用於執行數學任務。
Math 的屬性和方法的文法:
var pi_value=Math.PI;
var sqrt_value=Math.sqrt(15);
4.Math 對象方法
<script type="text/javascript">
document.write("PI圓周率:"+Math.PI+"<br>");
document.write("ceil上舍入(12.253):"+Math.ceil(12.253)+"<br>");
document.write("floor下舍入(12.253):"+Math.floor(12.253)+"<br>");
document.write("round四捨五入(12.253):"+Math.round(12.253)+"<br>");
document.write("round四捨五入(12.53):"+Math.round(12.53)+"<br>");
document.write("round四捨五入(-4.5):"+Math.round(-4.5)+"<br>");
document.write("round四捨五入(-4.2):"+Math.round(-4.2)+"<br>");
document.write("max最大值):"+Math.max(12,2,4,45,1)+"<br>");
document.write("max最大值(NAN)):"+Math.max("a",2,4,45,1)+"<br>");
document.write("min最小值):"+Math.min(12,2,4,45,-1)+"<br>");
document.write("min最小值(NAN)):"+Math.min("a",2,4,45,1)+"<br>");
document.write("random隨機數"+Math.random()+"<br>");
</script>
DATE日期··MATH對象