您好,登錄后才能下訂單哦!
1、js獲取當前日期(yyyy-mm-dd)
以下代碼是獲取到的當前日期:
var myDate = new Date(); var year = myDate.getFullYear(); //獲取完整的年份(4位,1970-????) var month = myDate.getMonth()+1; //獲取當前月份(1-12) var day = myDate.getDate(); //獲取當前日(1-31) //獲取完整年月日 var newDay = year + “-” + month + “-” + day;
2、點擊實現日期的天數加減(yyyy-mm-dd)
點擊倆個按鈕分別可以實現日期的加減,如果本月天數達到最多,那么月份將會自動增加或減少
var n = 0; dayChange(0) $("#time-add").click(function(){ n++; dayChange(n); }) $("#time-less").click(function(){ n--; dayChange(n); }) function dayChange(n){ var now = new Date();//今天 var tomo = new Date((now/1000+86400*n)*1000);//明天 var month = tomo.getMonth() + 1; var strDate = tomo.getDate(); var seperator1 = "-"; if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = tomo.getFullYear() + seperator1 + month + seperator1 + strDate; $(".center-day").html(currentdate); }
3、獲取當前本周周一和本周周日的時間范圍
不管當前是周幾,都可以獲取到當前所在這一周的起始時間
var now = new Date();//今天 week(now); function week(now){ var nowTime = now.getTime() ; var day = now.getDay(); var oneDayLong = 24*60*60*1000 ; //獲取本周所在周一 var MondayTime = nowTime - (day-1)*oneDayLong ; //獲取本周所在周末 var SundayTime = nowTime + (7-day)*oneDayLong ; //轉化日期 var monday = new Date(MondayTime); var sunday = new Date(SundayTime); var month = monday.getMonth() + 1; var strDate = monday.getDate(); var month2 = sunday.getMonth() + 1; var strDate1 = sunday.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (month2 >= 1 && month2 <= 9) { month2 = "0" + month2; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } if (strDate1 >= 0 && strDate1 <= 9) { strDate1 = "0" + strDate1; } currentdate = monday.getFullYear() + seperator1 + month + seperator1 + strDate + "至" + sunday.getFullYear() + seperator1 + month2 + seperator1 + strDate1; $(".center-day").html(currentdate); 32}
4、點擊實現每周范圍的變化
點擊改變的按鈕將會改變顯示一周范圍的改變,如果有的在下一月或者下一年,那么將會自動顯示,不會出現錯誤
var now = new Date();//今天 var n = 0; week(now); $("#week-add").click(function(){ n++; var date = new Date(now.getTime() + n*7*24*3600*1000); week(date); }) $("#week-add").click(function(){ n--; var date = new Date(now.getTime() + n*7*24*3600*1000); week(date); }) function week(now){ var nowTime = now.getTime() ; var day = now.getDay(); var oneDayLong = 24*60*60*1000 ; //獲取本周所在周一 var MondayTime = nowTime - (day-1)*oneDayLong ; //獲取本周所在周末 var SundayTime = nowTime + (7-day)*oneDayLong ; 22//轉化日期 var monday = new Date(MondayTime); var sunday = new Date(SundayTime); var month = monday.getMonth() + 1; var strDate = monday.getDate(); var month2 = sunday.getMonth() + 1; var strDate1 = sunday.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (month2 >= 1 && month2 <= 9) { month2 = "0" + month2; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } if (strDate1 >= 0 && strDate1 <= 9) { strDate1 = "0" + strDate1; } currentdate = monday.getFullYear() + seperator1 + month + seperator1 + strDate + "至" + sunday.getFullYear() + seperator1 + month2 + seperator1 + strDate1; $(".center-day").html(currentdate); }
5、獲取當前月份的第一天和最后一天
能夠獲取到當前所在月份的第一天和最后一天,最后一天的日期是不固定的,能夠獲取到應有的日期
monthfen(0) function monthfen(n){ var now = new Date();//今天 var firstDate = new Date((now/1000+86400*n*now.getDate())*1000);//明天 //本月第一天 firstDate.setDate(1); //第一天 var date = new Date(firstDate); var month = date.getMonth() + 1; var strDate = "0" + date.getDate(); //本月最后一天 var endDate = new Date(firstDate); endDate.setMonth(firstDate.getMonth()+1); endDate.setDate(0); var date1 = new Date(endDate); var month2 = date1.getMonth() + 1; var strDate1 = date1.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (month2 >= 1 && month2 <= 9) { month2 = "0" + month2; } currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + "至" + date1.getFullYear() + seperator1 + month2 + seperator1 + strDate1; $(".center-day").html(currentdate); }
6、點擊實現當前月份的改變
點擊按鈕會實現當前月份的改變,那么最后一天的日期也會自動改變,
monthfen(0) var n = 0; $("#month-add").click(function(){ n++; monthfen(n); }) $("#month-less").click(function(){ n--; monthfen(n); }) function monthfen(n){ var now = new Date();//今天 var firstDate = new Date((now/1000+86400*n*now.getDate())*1000);//明天 //本月第一天 firstDate.setDate(1); //第一天 var date = new Date(firstDate); var month = date.getMonth() + 1; var strDate = "0" + date.getDate(); //本月最后一天 var endDate = new Date(firstDate); endDate.setMonth(firstDate.getMonth()+1); endDate.setDate(0); var date1 = new Date(endDate); var month2 = date1.getMonth() + 1; var strDate1 = date1.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (month2 >= 1 && month2 <= 9) { month2 = "0" + month2; } currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + "至" + date1.getFullYear() + seperator1 + month2 + seperator1 + strDate1; $(".center-day").html(currentdate); }
當然還有很多關于日期格式的改變和算法,如果有什么不理解的可以留下評論,大家一起探討。
以上這篇js實現日期顯示的一些操作(實例講解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。