首页 > 代码库 > js日期函数
js日期函数
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>郭颖的测试</title> 5 <meta charset="utf8"> 6 </head> 7 8 <body> 9 </body> 10 <script src="jquery-3.0.0.js"></script> 11 <script> 12 var month,date,hours,minutes,seconds; 13 var curTime = new Date($.ajax({async: false}).getResponseHeader("Date")); 14 var curTime1 = new Date($.ajax({async: false}).getResponseHeader("Date")); 15 var curTime2 = new Date($.ajax({async: false}).getResponseHeader("Date")); 16 console.log(curTime); 17 function checkMonth(d){ 18 if(d.getMonth() < 10){ 19 month = ‘0‘+(d.getMonth()+1); 20 } else { 21 month = d.getMonth()+1; 22 } 23 return month; 24 } 25 function checkDate(d){ 26 if(d.getDate() < 10){ 27 date = ‘0‘+d.getDate(); 28 } else { 29 date = d.getDate(); 30 } 31 return date; 32 } 33 function checkHours(d){ 34 if(d.getHours() < 10){ 35 hours = ‘0‘+d.getHours(); 36 } else { 37 hours = d.getHours(); 38 } 39 return hours; 40 } 41 function checkMinutes(d){ 42 if(d.getMinutes() < 10){ 43 minutes = ‘0‘+d.getMinutes(); 44 } else { 45 minutes = d.getMinutes(); 46 } 47 return minutes; 48 } 49 function checkSeconds(d){ 50 if(d.getSeconds() < 10){ 51 seconds = ‘0‘+d.getSeconds(); 52 } else { 53 seconds = d.getSeconds(); 54 } 55 return seconds; 56 } 57 //获取今天的日期时间,str决定格式,常见可选‘‘或‘-‘ 58 function getCurDateTime(d,str) { 59 checkMonth(d); 60 checkDate(d); 61 checkHours(d); 62 checkMinutes(d); 63 checkSeconds(d); 64 if(str=="-"){ 65 var time = d.getFullYear() + str + month + str + date + " " + hours + ":" + minutes + ":" + seconds; 66 } else if(str==‘‘){ 67 var time = d.getFullYear() + str + month + str + date + str + hours + str + minutes + str + seconds; 68 } 69 return time; 70 } 71 //获取今天的日期,str决定格式,常见可选‘‘或‘-‘ 72 function getCurDate(d,str) { 73 checkMonth(d); 74 checkDate(d); 75 var time = d.getFullYear() + str + month + str + date; 76 return time; 77 } 78 //获取今天的时间,str决定格式,常见可选‘‘或‘-‘ 79 function getCurTime(d,str){ 80 checkHours(d); 81 checkMinutes(d); 82 checkSeconds(d); 83 var time = hours + str + minutes + str + seconds; 84 return time; 85 } 86 //获取AddDayCount天后的日期,str决定格式,常见可选‘‘或‘-‘ 87 function GetDateStr(dd,AddDayCount,str) { 88 var guo = dd; 89 guo.setDate(guo.getDate()+AddDayCount); 90 var y = guo.getFullYear(); 91 var m = checkMonth(guo);//获取当前月份的日期 92 var d = checkDate(guo); 93 var result = y+str+m+str+d; 94 return result; 95 } 96 var todayAll = getCurDateTime(curTime,‘‘); 97 var today_All = getCurDateTime(curTime,‘-‘); 98 var nowPart = getCurTime(curTime,‘‘); 99 var now_Part = getCurTime(curTime,‘:‘); 100 var datePart = getCurDate(curTime,‘‘); 101 var date_Part = getCurDate(curTime,‘-‘); 102 var yesterdayPart = GetDateStr(curTime1,-1,‘‘); 103 var tomorrowPart = GetDateStr(curTime2,+1,‘‘); 104 console.log(todayAll); 105 console.log(today_All); 106 console.log(nowPart); 107 console.log(now_Part); 108 console.log(datePart); 109 console.log(date_Part); 110 console.log(yesterdayPart); 111 console.log(tomorrowPart); 112 var time1 = parseInt(curTime.getTime()/1000); 113 console.log(time1); 114 var date2 = date_Part+" 13:09:00"; 115 console.log(date2); 116 date2 = new Date(Date.parse(date2.replace(/-/g, "/"))); 117 console.log(date2); 118 var time2 = parseInt(date2.getTime()/1000); 119 var date3 = date_Part+" 04:04:00"; 120 console.log(date3); 121 date3 = new Date(Date.parse(date3.replace(/-/g, "/"))); 122 console.log(date3); 123 var l_tx = yesterdayPart+‘180‘; 124 var n_tx = datePart+‘001‘; 125 // var ll_tx = myDate 126 console.log(l_tx); 127 console.log(n_tx); 128 </script> 129 </html>
js日期函数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。