時間對象是一個我們經常要用到的對象,無論是做時間輸出、時間判斷等操作時都與這個對象離不開。除開JavaScript中的時間對象外,在VbScript中也有許多的時間對象,而且非常好用。下面還是按照我們的流程來進行講解。
它是一個內置對象——而不是其它對象的屬性,允許用戶執行各種使用日期和時間的過程。
方法:分為得到時間方法、設置時間方法和轉換時間方法
得到時間方法:
getDate() 查看Date對象并返回日期
getDay() 返回星期幾
getHours() 返回小時數
getMinutes() 返回分鐘數
getMonth() 返回月份值
getSeconds() 返回秒數
getTime() 返回完整的時間
getYear() 返回年份
設置時間方法:
setDate() 改變Date對象的日期
setHours() 改變小時數
setMinutes() 改變分鐘數
setMonth() 改變月份
setSeconds() 改變秒數
setTime() 改變完整的時間
setYear() 改變年份
轉換時間方法:
toGMTString() 把Date對象的日期(一個數值)轉變成一個GMT時間字符串,返回類似下面的值:Weds,15 June l997 14:02:02 GMT(精確的格式依賴于計算機上所運行的操作系統而變)
toLocaleString() 把Date對象的日期(一個數值)轉變成一個字符串,使用所在計算機上配置使用的特定日期格式
UTC() 使用Date UTC(年、月、日、時、分、秒),以自從1970年1月1日00:00:00(其中時、分、秒是可選的)以來的毫秒數的形式返回日期
幾個需要注意的地方:
1、得到日期和年和設置日期和年時間,其中很怪的問題就是不能對月份進行設置(比較的怪):
<script language="javascript">
d = new Date();
alert(d.toLocaleString());
d.setDate(25);
alert(d.toLocaleString());
d.setYear(2000);
alert(d.toLocaleString());
</script>
2、獲得年的時候最好用getFullYear()方法來做
3、由于針對月份,JS是從0開始的,因此需要對月份進行操作時要加1
下面是幾個關于時間的經典而且經常會用到的例子,希望對大家會有提高的。謝謝繼續關注該帖子。。。
1、將2005-8-5轉換成2005-08-05格式
<script language="javascript">
var strDate = '2005-8-5';
window.alert(strDate.replace(/\b(\w)\b/g, '0$1'));
</script>
2、得到間隔天數
<script type="text/javascript">
<!--
alert("間隔天數為:"+(new Date('2005/8/15')-new Date('2003/9/18'))/1000/60/60/24+"天")
//-->
</script>
3、得到間隔時間
<script>
var d1=new Date("2004/09/16 20:08:00");
var d2=new Date("2004/09/16 10:18:03");
var d3=d1-d2;
var h=Math.floor(d3/3600000);
var m=Math.floor((d3-h*3600000)/60000);
var s=(d3-h*3600000-m*60000)/1000;
alert("相差"+h+"小時"+m+"分"+s+"秒");
</script>
4、得到今天的日期
<script language="javascript">
d = new Date();
alert(d.getFullYear()+"年"+(d.getMonth()+1)+"月"+d.getDate()+"日");
</script>
6、數字日期轉漢字
<html>
<head>
<title> New Document </title>
</head>
<body>
<script language=javascript>
Date.prototype.getRead = function()
{
var values = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九");
var returnValue, temp;
returnValue = this.getYear()+"年";
temp = (this.getMonth()+1)+"月"+this.getDate()+"日";
temp = temp.replace(/(\d)(\d)/g,"$1十$2").replace(/1十/g,"十").replace(/十0/g,"十");
returnValue += temp;
returnValue = returnValue.replace(/\d/g, function(sts){return values[parseInt(sts)]});
return returnValue;
}
var t=new Date();
document.write(t.getRead());
</script>
</body>
</html>
7、得到前N天或后N天的日期
方法一:
<script type="text/javascript">
function showdate(n)
{
var uom = new Date(new Date()-0+n*86400000);
uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate();
return uom;
}
window.alert("今天是:"+showdate(0));
window.alert("昨天是:"+showdate(-1));
window.alert("明天是:"+showdate(1));
window.alert("10天前是:"+showdate(-10));
window.alert("5天后是:"+showdate(5));
</script>
方法二:
<script type="text/javascript">
function showdate(n)
{
var uom = new Date();
uom.setDate(uom.getDate()+n);
uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate();
return uom;
}
window.alert("今天是:"+showdate(0));
window.alert("昨天是:"+showdate(-1));
window.alert("明天是:"+showdate(1));
window.alert("10天前是:"+showdate(-10));
window.alert("5天后是:"+showdate(5));
</script>
方法三(不好意思,這個市用vsscript做的):
<script language="vbscript">
function showdate(n)
showdate=dateadd("d",date(),n)
end function
msgbox "今天是:"&showdate(0)
msgbox "昨天是:"&showdate(-1)
msgbox "明天是:"&showdate(1)
msgbox "十天前是:"&showdate(-10)
msgbox "五天后是:"&showdate(5)
</script>
方法四:
<script language="Javascript">
Date.prototype.getDays=function(){
var _newDate=new Date();
_newDate.setMonth(_newDate.getMonth()+1);
_newDate.setDate(0);
$_days=_newDate.getDate();
delete _newDate;
return $_days;
}
function showdate(n)
{
var uom = new Date();
uom.setDate(uom.getDate()+n);
uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" +
uom.getDate()+"\n星期"+('天一二三四五六'.charAt(uom.getDay()))+"\n本月有"+
uom.getDays()+"天";
return uom;
}
window.alert("今天是:"+showdate(0));
window.alert("昨天是:"+showdate(-1));
window.alert("明天是:"+showdate(1));
window.alert("10天前是:"+showdate(-10));
window.alert("5天后是:"+showdate(5));
</script>
posted on 2008-01-12 10:36
無聲 閱讀(20922)
評論(0) 編輯 收藏 所屬分類:
職場生活