JavaScript獲取當(dāng)前時間向前推三個月的方法示例
本文實例講述了JavaScript獲取當(dāng)前時間向前推三個月的方法。分享給大家供大家參考,具體如下:
<!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ù)學(xué)運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
echarts加載區(qū)域地圖并標(biāo)注點的簡單步驟
這篇文章提供了南海區(qū)域地圖加載以及氣象站點標(biāo)注的詳細(xì)步驟,首先通過DataV.GeoAtlas地理小工具下載南海區(qū)域的JSON地圖文件,接著使用Echarts進(jìn)行地圖的展示,可以選擇是否顯示省下所有市的名字,需要的朋友可以參考下2024-09-09