asp做網(wǎng)站的時候可能需要計算兩個時間的間隔,比如在做新聞列表的時候,最后3天發(fā)布的新聞后面加個“new”之類的,都要計算一下時間間隔,這個時候就要用到計算時間間隔函數(shù)DateAdd。
DateAdd函數(shù)可以返回已添加指定時間間隔的日期。在向指定日期加上一段時間的基礎上,返回新的 datetime 值。
該函數(shù)的格式是:DateAdd(interval, number, date)
DateAdd 函數(shù)不會返回無效日期。如下示例將 95 年 1 月 31 日加上一個月:
NewDate = DateAdd("m", 1, "31-Jan-95")在這個示例中,DateAdd 返回 95 年 2 月 28 日,而不是 95 年 2 月 31 日。如果 date 為 96 年 1 月 31 日,則返回 96 年 2 月 29 日,這是因為 1996 是閏年。
如果計算的日期是在公元 100 年之前,則會產生錯誤。
如果 number 不是 Long 型值,則在計算前四舍五入為最接近的整數(shù)。
DateAdd使用的一些例子
給當前時間加一個月:如果now()是2009-7-8 10:02:50
response.write DateAdd("m",1,now()) ’返回結果是:2009-8-8 10:02:50
response.write DateAdd("m",1,date()) ’返回結果是:2009-8-8
給當前時間加45分鐘
response.write DateAdd("n",45,now()) ’返回結果是:2009-7-8 22:47:19
當前日期減一個月
response.write DateAdd("m",-1,date()) ’返回結果是:2009-6-8