javascript顯示上周、上個(gè)月日期的處理方法
本文實(shí)例介紹了javascript一周前、一個(gè)月前的實(shí)現(xiàn)代碼,對(duì)于javascript日期處理進(jìn)行了簡單分析,分享給大家供大家參考,具體內(nèi)容如下
<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();//點(diǎn)擊事件觸發(fā)
})
//專門包裝點(diǎn)擊事件;
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()));
}
})
}
//處理日期的函數(shù),返回一個(gè)字面量;
function ProcessDate(type) {
//1.0獲取現(xiàn)在時(shí)間的年月日:
var currentTime = new Date("2016-01-02"); //得到當(dāng)前的時(shí)間
var currentYear = currentTime.getFullYear(); //得到當(dāng)前的年份
var currentMoon = currentTime.getMonth() + 1; //得到當(dāng)前的月份(系統(tǒng)默認(rèn)為0-11,所以要加1才算是當(dāng)前的月份)
var currentDay = currentTime.getDate(); //得到當(dāng)前的天數(shù)
//2.0獲取當(dāng)前時(shí)間的一個(gè)月內(nèi)的年月日:(一個(gè)月內(nèi)的大眾業(yè)務(wù)需求為:當(dāng)前時(shí)間的月份-1,當(dāng)前時(shí)間的天數(shù)+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(); //獲取上個(gè)月的總天數(shù)
break;
case 7:
agoDay = currentDay - 6;
if (agoDay < 0) {
agoMoon = currentMoon - 1;//月份減1
max = new Date(agoYear, agoMoon, 0).getDate(); //獲取上個(gè)月的總天數(shù)
agoDay = max + agoDay;//天數(shù)在上個(gè)月的總天數(shù)的基礎(chǔ)上減去負(fù)數(shù)
}
break;
}
//3.0對(duì)處理的年月日作邏輯判斷
//如果beginDay > max(如果是當(dāng)前時(shí)間的天數(shù)+1后的數(shù)值超過了上個(gè)月的總天數(shù): 天數(shù)變?yōu)?,月份增加1)
if (agoDay > max) {
agoDay = 1;
agoMoon += 1;
}
//如果月份當(dāng)月為1月的時(shí)候, 那么一個(gè)月內(nèi): 年:-1 月:12 日:依然不變
if (agoMoon == 0) {
agoMoon = 12;
agoYear = currentYear - 1;
}
//4.0對(duì)已經(jīng)處理好的數(shù)據(jù)作格式處理(單位數(shù)則自動(dòng)補(bǔ)零)
currentMoon = Appendzero(currentMoon);
currentDay = Appendzero(currentDay);
agoMoon = Appendzero(agoMoon);
agoDay = Appendzero(agoDay);
//5.0幫助代碼
console.log("當(dāng)前時(shí)間為:{0}-{1}-{2}".format(currentYear, currentMoon, currentDay));
console.log("一個(gè)月前的時(shí)間為{0}-{1}-{2}".format(agoYear, agoMoon, agoDay));
return { "Year": agoYear, "Moon": agoMoon, "Day": agoDay };
}
//處理各位數(shù)為零的數(shù)字(單位數(shù)則加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="一個(gè)月內(nèi)"/>
<input type="button" class="tbBtn" id="CbtnWeek" style="width: 80px; margin-left: 5px; margin-right: 5px;" value="一周內(nèi)"/>
<input id = "txtCallCycleBegin" type="text"/>
<input id = "txtCallCyclecurrend" type="text"/>
</body>
</html>
以上就是本文的全部內(nèi)容,希望能夠幫助大家更好的解決javascript日期處理問題。
- js 取時(shí)間差去掉周六周日實(shí)現(xiàn)代碼
- js獲取當(dāng)前周、上一周、下一周日期
- js獲取時(shí)間(本周、本季度、本月..)
- javascript 當(dāng)前日期加(天、周、月、年)
- js實(shí)現(xiàn)獲取當(dāng)前時(shí)間是本月第幾周的方法
- JS獲取月份最后天數(shù)、最大天數(shù)與某日周數(shù)的方法
- JS 操作日期 順便實(shí)現(xiàn) 上一周 和 下一周 功能
- JavaScript 計(jì)算當(dāng)天是本年本月的第幾周
- JS獲取本周周一,周末及獲取任意時(shí)間的周一周末功能示例
- js輸出陰歷、陽歷、年份、月份、周示例代碼
- JS實(shí)現(xiàn)獲取當(dāng)前所在周的周六、周日示例分析
相關(guān)文章
微信小程序報(bào)錯(cuò):does?not?have?a?method?"xxxx"?to?ha
這篇文章主要給大家介紹了關(guān)于微信小程序報(bào)錯(cuò):does?not?have?a?method?"xxxx"?to?handle?event?"tap"的解決方案,文中將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
js運(yùn)動(dòng)動(dòng)畫的八個(gè)知識(shí)點(diǎn)
本文給大家分享的是個(gè)人在學(xué)習(xí)js運(yùn)動(dòng)動(dòng)畫的過程中總結(jié)的我們需要注意的8個(gè)知識(shí)點(diǎn),分別是速度動(dòng)畫、透明度漸變、緩沖運(yùn)動(dòng)、多物體運(yùn)動(dòng)、獲取樣式、任意屬性值、鏈?zhǔn)竭\(yùn)動(dòng)和同時(shí)運(yùn)動(dòng),非常的詳細(xì),推薦給小伙伴們。2015-03-03
調(diào)試JavaScript中正則表達(dá)式中遇到的問題
這篇文章主要介紹了調(diào)試JavaScript中正則表達(dá)式中遇到的問題,需要的朋友可以參考下2015-01-01
javascript 函數(shù)的暫停和恢復(fù)實(shí)例詳解
這篇文章主要介紹了javascript 函數(shù)的暫停和恢復(fù),結(jié)合實(shí)例形式詳細(xì)分析了javascript 函數(shù)的暫停和恢復(fù)相關(guān)原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
layui(1.0.9)文件上傳upload,前后端的實(shí)例代碼
今天小編就為大家分享一篇layui(1.0.9)文件上傳upload,前后端的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09
javascript實(shí)現(xiàn)移動(dòng)端輪播圖
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)移動(dòng)端輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12

