計算天數,加小時,加分數
Date.prototype.Format = function (fmt) { // author: meizz var o = { "M+": this.getMonth() + 1, // 月份 "d+": this.getDate(), // 日 "h+": this.getHours(), // 小時 "m+": this.getMinutes(), // 分 "s+": this.getSeconds(), // 秒 "q+": Math.floor((this.getMonth() + 3) / 3), // 季度 "S": this.getMilliseconds() // 毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } var nowTime = new Date().Format("yyyy-MM-dd hh:mm:ss"); //當前時間 //console.log(nowTime) var tenMinutes= new Date(nowTime); tenMinutes.setMinutes(tenMinutes.getMinutes()-30); //當前時間加10分鍾 var tenmin = tenMinutes.Format("yyyy-MM-dd hh:mm:ss"); <script type="text/javascript"> function f() { var a = "2019-07-11T16:54:36.193"; var b = a.replace("T"," "); var c = b.substring(0,16) console.log(c); } //時間提取處理 f(); var date1= '2019-07-01 01:09:00'; //開始時間 var date2 = '2019-07-11 02:12:00'; //結束時間 var date3 = new Date(date2).getTime() - new Date(date1).getTime(); //時間差的毫秒數 //計算出相差天數 var days=Math.floor(date3/(24*3600*1000)) //計算出小時數 var leave1=date3%(24*3600*1000) //計算天數后剩余的毫秒數 var hours=Math.floor(leave1/(3600*1000)) //計算相差分鍾數 var leave2=leave1%(3600*1000) //計算小時數后剩余的毫秒數 var minutes=Math.floor(leave2/(60*1000)) //計算相差秒數 var leave3=leave2%(60*1000) //計算分鍾數后剩余的毫秒數 var seconds=Math.round(leave3/1000) console.log(" 相差 "+days+"天 "+hours+"小時 "+minutes+" 分鍾"+seconds+" 秒") console.log(Math.round(((days*24)+hours)/30)) </script> <script> //GMT轉普通格式的方法 function GMTToStr(time){ let date = new Date(time) let Str=date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds() return Str } function judgFailTime() { var x = "2019-07-18 23:25:26"; var times = new Date(x.replace("-","/")); var b = 15; //分鍾數 var c = 5; //小時數 times.setHours(times.getHours()+c); //小時 //times.setMinutes(times.getMinutes() + b), //分種 //times.getSeconds(), 0); //秒數 //console.log(times); //GMT轉普通格式 let DateTime=times; let a=this.GMTToStr(DateTime); console.log(a); } judgFailTime() </script> <script> var stringTime = '2019-07-15T11:08:13.681648'; var timestamp2 = Date.parse(new Date(stringTime)); timestamp2 = timestamp2 / 1000; var seconds = 2*3600; timestamp=timestamp2+seconds console.log(stringTime + "的時間戳為:" + timestamp); var newDate = new Date(); newDate.setTime(timestamp * 1000); console.log(newDate.toGMTString()); </script> <script> //2019-07-10 10:10 var mydate= new Date("2019-07-10 10:10"); mydate.setDate(mydate.getDate()+1); //當前時間加1天 //console.log(mydate) mydate.setMinutes(mydate.getMinutes()+20); //當前時間加20分鍾 mydate.setHours(mydate.getHours()+2) //當前時間加上小時 //console.log(mydate) </script>