JavaScript獲取當前時間向前推三個月的方法示例
本文實例講述了JavaScript獲取當前時間向前推三個月的方法。分享給大家供大家參考,具體如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script type="text/javascript" language="javascript" >
function get3MonthBefor(){
var resultDate,year,month,date,hms;
var currDate = new Date();
year = currDate.getFullYear();
month = currDate.getMonth()+1;
date = currDate.getDate();
hms = currDate.getHours() + ':' + currDate.getMinutes() + ':' + (currDate.getSeconds() < 10 ? '0'+currDate.getSeconds() : currDate.getSeconds());
switch(month)
{
case 1:
case 2:
case 3:
month += 9;
year--;
break;
default:
month -= 3;
break;
}
month = (month < 10) ? ('0' + month) : month;
resultDate = year + '-'+month+'-'+date+' ' + hms;
return resultDate;
}
document.write(get3MonthBefor());
</script>
</head>
<body>
</body>
</html>
運行效果圖如下:

PS:這里再為大家推薦幾款時間及日期相關(guān)工具供大家參考使用:
在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線日期計算器/相差天數(shù)計算器:
http://tools.jb51.net/jisuanqi/datecalc
在線日期天數(shù)差計算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix時間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript時間與日期操作技巧總結(jié)》《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
javascript實現(xiàn)頁面內(nèi)關(guān)鍵詞高亮顯示代碼
關(guān)鍵詞高亮想必大家對它都不陌生吧,應(yīng)用也比較廣泛的,下面為大家介紹下通過javascript是如何實現(xiàn)頁面內(nèi)關(guān)鍵詞高亮顯示2014-04-04
javascript中的Base64、UTF8編碼與解碼詳解
本文給大家介紹的是javascript中的Base64、UTF8編碼與解碼的函數(shù)源碼分享以及使用范例,十分實用,推薦給小伙伴們,希望大家能夠喜歡。2015-03-03
JavaScript基礎(chǔ)之this和箭頭函數(shù)詳析
這篇文章主要給大家介紹了關(guān)于JavaScript基礎(chǔ)之this和箭頭函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用JavaScript具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-09-09

