javascript顯示上周、上個月日期的處理方法,javascript上周
本文執行個體介紹了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日期處理問題。
您可能感興趣的文章:
- javascript 當前日期加(天、周、月、年)
- JS 操作日期 順便實現 上一周 和 下一周 功能
- js擷取當前月的第一天和最後一天的小例子
- moment.js輕鬆實現擷取當前日期是當年的第幾周
- js日期範圍初始化得到前一個月日期的方法
- js實現擷取目前時間是本月第幾周的方法