本日時間
二七ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
function showToDay() { var Nowdate=new Date(); M=Number(Nowdate.getMonth())+1 alert(Nowdate.getMonth()+"月,"+Nowdate.getDate()+"號,星期"+Nowdate.getDay()); return Nowdate.getYear()+"-"+M+"-"+Nowdate.getDate(); }
昨天
function showTomorrow() { var tom=new Date(); tom.setDate(tom.getDate()+1); M=Number(tom.getMonth())+1 return tom.getYear()+"-"+M+"-"+tom.getDate(); }
本周第一天
function showWeekFirstDay() { var Nowdate=new Date(); var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000); M=Number(WeekFirstDay.getMonth())+1 return WeekFirstDay.getYear()+"-"+M+"-"+WeekFirstDay.getDate(); }
本周最后天
function showWeekLastDay() { var Nowdate=new Date(); var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000); var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000); M=Number(WeekLastDay.getMonth())+1 return WeekLastDay.getYear()+"-"+M+"-"+WeekLastDay.getDate(); }
本月第一天
function showMonthFirstDay() { var Nowdate=new Date(); var MonthFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth(),1); M=Number(MonthFirstDay.getMonth())+1 return MonthFirstDay.getYear()+"-"+M+"-"+MonthFirstDay.getDate(); }
本月最后一天
function showMonthLastDay() { var Nowdate=new Date(); var MonthNextFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth()+1,1); var MonthLastDay=new Date(MonthNextFirstDay-86400000); M=Number(MonthLastDay.getMonth())+1 return MonthLastDay.getYear()+"-"+M+"-"+MonthLastDay.getDate(); } function getTime(s) { var time1=document.getElementById("num1"); var time2=document.getElementById("num2"); if(s==1){ //本日時間 time1.value=showToDay(); //alert("今日:"+time1.value); time2.value=showTomorrow(); //alert("今日:"+time2.value); }else if(s==2){ //本周時間 time1.value=showWeekFirstDay(); //alert("本月第一天:"+time1.value); time2.value=showWeekLastDay(); //alert("本月最后一天:"+time2.value); }else{ //本月時間 time1.value=showMonthFirstDay(); //alert("本月第一天"+time1.value); time2.value=showMonthLastDay(); //alert("本月最后一天"+time2.value); } }
其他網(wǎng)友補充的完整測試代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <input type="text" id="dateFrom" value=""> <input type="text" id="dateTo" value=""> <script> window.onload = function() { // getDate("thisDay"); // getDate("thisWeek"); // getDate("thisMonth"); getDate("thisYear"); } //獲取推送時間段 function getDate(type){ var kssj,jssj; var today = new Date(); var DAY = 1000*60*60*24; if(type =="thisDay") { //當天 kssj = new Date(); jssj = new Date(); } if(type == "thisWeek") { //本周周 kssj = getThisWeekFirstDay();//本周第一天 jssj = new Date(); } if(type == "thisMonth") { //本月 kssj = getThisMonthFirstDay();//本月第一天 jssj = new Date(); } if(type == "thisYear") { kssj = getThisYearFirstDay();//本年第一天 jssj = new Date(); } var dateFrom = kssj.format('yyyy-MM-dd'); var dateTo = jssj.format('yyyy-MM-dd'); document.getElementById("dateFrom").value=dateFrom; document.getElementById("dateTo").value=dateTo; } function getThisWeekFirstDay() { var now = new Date(); var day = now.getDay(); var week = "1234567"; var first = 0 - week.indexOf(day); var thisWeekfirstDay = new Date; thisWeekfirstDay.setDate (thisWeekfirstDay.getDate () + first); return thisWeekfirstDay; } function getThisMonthFirstDay(){ var date=new Date(); date.setDate(1); return date; } function getThisYearFirstDay() { var currentDate=new Date(); var currentYear=currentDate.getFullYear(); var currentYearFirstDate=new Date(currentYear,0,1); return currentYearFirstDate; } //data類型轉(zhuǎn)string Date.prototype.format = function(format) { var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond } if(/(y+)/.test(format)) format=format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); for(var k in o)if(new RegExp("("+ k +")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); return format; } </script> </body> </html>
這樣基本上就解決問題了
網(wǎng)頁標題:js獲取本日、本周、本月的時間代碼
網(wǎng)站鏈接:http://jinyejixie.com/article42/jdooec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、App設(shè)計、Google、云服務(wù)器、全網(wǎng)營銷推廣、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)