本文實例介紹了javascript一周前、一個月前的實現代碼,對於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();//點擊事件觸發
})
//專門包裝點擊事件;
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獲取現在時間的年月日:
var currentTime = new Date("2016-01-02"); //得到當前的時間
var currentYear = currentTime.getFullYear(); //得到當前的年份
var currentMoon = currentTime.getMonth() + 1; //得到當前的月份(系統默認為0-11,所以要加1才算是當前的月份)
var currentDay = currentTime.getDate(); //得到當前的天數
//2.0獲取當前時間的一個月內的年月日:(一個月內的大眾業務需求為:當前時間的月份-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後的數值超過了上個月的總天數: 天數變為1,月份增加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日期處理問題。