javascript顯示上周、上個月日期的處理方法
更新時間:2016年02月03日 16:49:42 作者:歐歐歐鋒_
這篇文章主要為大家分享了關于javascript實現(xiàn)上周、上個月的處理方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例介紹了javascript一周前、一個月前的實現(xiàn)代碼,對于javascript日期處理進行了簡單分析,分享給大家供大家參考,具體內容如下
<html>
<head>
<title></title>
<script src="../Script/jQuery/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="../Script/MTHCRMWidget/MTHCRMWidget.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
myClick();//點擊事件觸發(fā)
})
//專門包裝點擊事件;
function myClick() {
$(".tbBtn").click(function () {
var sid = $(this).attr("id");
var agoDate = "";
var Cdate = new Date();
if (sid == "CbtnNull") {
$("#txtCallCycleBegin").val("");
$("#txtCallCyclecurrend").val("");
} else if (sid == "CbtnMoon") {
agoDate = ProcessDate(30);
$("#txtCallCycleBegin").val("{0}-{1}-{2}".format(agoDate.Year, agoDate.Moon, agoDate.Day));
$("#txtCallCyclecurrend").val("{0}-{1}-{2}".format(Cdate.getFullYear(), Cdate.getMonth() + 1, Cdate.getDate()));
} else {
agoDate = ProcessDate(7);
$("#txtCallCycleBegin").val("{0}-{1}-{2}".format(agoDate.Year, agoDate.Moon, agoDate.Day));
$("#txtCallCyclecurrend").val("{0}-{1}-{2}".format(Cdate.getFullYear(), Cdate.getMonth() + 1, Cdate.getDate()));
}
})
}
//處理日期的函數,返回一個字面量;
function ProcessDate(type) {
//1.0獲取現(xiàn)在時間的年月日:
var currentTime = new Date("2016-01-02"); //得到當前的時間
var currentYear = currentTime.getFullYear(); //得到當前的年份
var currentMoon = currentTime.getMonth() + 1; //得到當前的月份(系統(tǒng)默認為0-11,所以要加1才算是當前的月份)
var currentDay = currentTime.getDate(); //得到當前的天數
//2.0獲取當前時間的一個月內的年月日:(一個月內的大眾業(yè)務需求為:當前時間的月份-1,當前時間的天數+1)
var agoDay = "";
var agoMoon = currentMoon;
var agoYear = currentYear;
var max = "";
switch (type) {
case 30:
agoDay = currentDay + 1;
agoMoon = currentMoon - 1;
max = new Date(agoYear, agoMoon, 0).getDate(); //獲取上個月的總天數
break;
case 7:
agoDay = currentDay - 6;
if (agoDay < 0) {
agoMoon = currentMoon - 1;//月份減1
max = new Date(agoYear, agoMoon, 0).getDate(); //獲取上個月的總天數
agoDay = max + agoDay;//天數在上個月的總天數的基礎上減去負數
}
break;
}
//3.0對處理的年月日作邏輯判斷
//如果beginDay > max(如果是當前時間的天數+1后的數值超過了上個月的總天數: 天數變?yōu)?,月份增加1)
if (agoDay > max) {
agoDay = 1;
agoMoon += 1;
}
//如果月份當月為1月的時候, 那么一個月內: 年:-1 月:12 日:依然不變
if (agoMoon == 0) {
agoMoon = 12;
agoYear = currentYear - 1;
}
//4.0對已經處理好的數據作格式處理(單位數則自動補零)
currentMoon = Appendzero(currentMoon);
currentDay = Appendzero(currentDay);
agoMoon = Appendzero(agoMoon);
agoDay = Appendzero(agoDay);
//5.0幫助代碼
console.log("當前時間為:{0}-{1}-{2}".format(currentYear, currentMoon, currentDay));
console.log("一個月前的時間為{0}-{1}-{2}".format(agoYear, agoMoon, agoDay));
return { "Year": agoYear, "Moon": agoMoon, "Day": agoDay };
}
//處理各位數為零的數字(單位數則加0)
function Appendzero(obj) {
if (obj < 10) {
return "0" + obj;
} else {
return obj;
}
}
</script>
</head>
<body>
<input type="button" class="tbBtn" id="CbtnNull" style="background-color:#e3e3e3" value="不限"/>
<input type="button" class="tbBtn" id="CbtnMoon" style="width: 80px; margin-left: 5px; margin-right: 5px;" value="一個月內"/>
<input type="button" class="tbBtn" id="CbtnWeek" style="width: 80px; margin-left: 5px; margin-right: 5px;" value="一周內"/>
<input id = "txtCallCycleBegin" type="text"/>
<input id = "txtCallCyclecurrend" type="text"/>
</body>
</html>
以上就是本文的全部內容,希望能夠幫助大家更好的解決javascript日期處理問題。
相關文章
JS 非圖片動態(tài)loading效果實現(xiàn)代碼
功能說明:譬如在按某個button時,顯示消息"Loading”,然后每隔一秒后后面加上".",至一定數量的"."時如:"Loading...",再重置此消息為"Loading",繼續(xù)動態(tài)顯示,直至按鈕事件處理完成。2010-04-04
jQuery?事件綁定及取消?bind?live?delegate?on?one區(qū)別解析
這篇文章主要介紹了jquery?事件綁定及取消?bind?live?delegate?on?one區(qū)別解析,本文給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11

