检查Javascript中的时差

检查Javascript中的时差

你如何检查Javascript中两个文本框的时差?



慕运维8079593
浏览 415回答 3
3回答

收到一只叮咚

凑合。减去JavaScript Date对象以获得它们的区别:// use a constant date (e.g. 2000-01-01) and the desired time to initialize two datesvar date1 = new Date(2000, 0, 1,&nbsp; 9, 0); // 9:00 AMvar date2 = new Date(2000, 0, 1, 17, 0); // 5:00 PM// the following is to handle cases where the times are on the opposite side of// midnight e.g. when you want to get the difference between 9:00 PM and 5:00 AMif (date2 < date1) {&nbsp; &nbsp; date2.setDate(date2.getDate() + 1);}var diff = date2 - date1;// 28800000 milliseconds (8 hours)然后,您可以将毫秒转换为小时,分钟和秒,如下所示:var msec = diff;var hh = Math.floor(msec / 1000 / 60 / 60);msec -= hh * 1000 * 60 * 60;var mm = Math.floor(msec / 1000 / 60);msec -= mm * 1000 * 60;var ss = Math.floor(msec / 1000);msec -= ss * 1000;// diff = 28800000 => hh = 8, mm = 0, ss = 0, msec = 0您可以将字符串转换为24小时格式,如下所示:function parseTime(s) {&nbsp; &nbsp; var part = s.match(/(\d+):(\d+)(?: )?(am|pm)?/i);&nbsp; &nbsp; var hh = parseInt(part[1], 10);&nbsp; &nbsp; var mm = parseInt(part[2], 10);&nbsp; &nbsp; var ap = part[3] ? part[3].toUpperCase() : null;&nbsp; &nbsp; if (ap === "AM") {&nbsp; &nbsp; &nbsp; &nbsp; if (hh == 12) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hh = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (ap === "PM") {&nbsp; &nbsp; &nbsp; &nbsp; if (hh != 12) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hh += 12;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return { hh: hh, mm: mm };}parseTime("12:00 AM"); // {hh:&nbsp; 0, mm: 0}parseTime("12:00 PM"); // {hh: 12, mm: 0}parseTime("01:00 PM"); // {hh: 13, mm: 0}parseTime("23:00");&nbsp; &nbsp; // {hh: 23, mm: 0}

一只萌萌小番薯

此函数返回一个字符串,其中包含与datetime字符串和当前日期时间的差异。function&nbsp;get_time_diff(&nbsp;datetime&nbsp;){ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;datetime&nbsp;=&nbsp;typeof&nbsp;datetime&nbsp;!==&nbsp;'undefined'&nbsp;?&nbsp;datetime&nbsp;:&nbsp;"2014-01-01&nbsp;01:02:03.123456"; &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;datetime&nbsp;=&nbsp;new&nbsp;Date(&nbsp;datetime&nbsp;).getTime(); &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;now&nbsp;=&nbsp;new&nbsp;Date().getTime(); &nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;isNaN(datetime)&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;""; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;console.log(&nbsp;datetime&nbsp;+&nbsp;"&nbsp;"&nbsp;+&nbsp;now); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(datetime&nbsp;<&nbsp;now)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;milisec_diff&nbsp;=&nbsp;now&nbsp;-&nbsp;datetime; &nbsp;&nbsp;&nbsp;&nbsp;}else{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;milisec_diff&nbsp;=&nbsp;datetime&nbsp;-&nbsp;now; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;days&nbsp;=&nbsp;Math.floor(milisec_diff&nbsp;/&nbsp;1000&nbsp;/&nbsp;60&nbsp;/&nbsp;(60&nbsp;*&nbsp;24)); &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;date_diff&nbsp;=&nbsp;new&nbsp;Date(&nbsp;milisec_diff&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;days&nbsp;+&nbsp;"&nbsp;Days&nbsp;"+&nbsp;date_diff.getHours()&nbsp;+&nbsp;"&nbsp;Hours&nbsp;"&nbsp;+&nbsp;date_diff.getMinutes()&nbsp;+&nbsp;"&nbsp;Minutes&nbsp;"&nbsp;+&nbsp;date_diff.getSeconds()&nbsp;+&nbsp;"&nbsp;Seconds";}在谷歌浏览器中测试console(按F12)get_time_diff()1388534523123&nbsp;1375877555722"146&nbsp;Days&nbsp;12&nbsp;Hours&nbsp;49&nbsp;Minutes&nbsp;27&nbsp;Seconds"

眼眸繁星

这是适用于我的解决方案:var&nbsp;date1&nbsp;=&nbsp;new&nbsp;Date("08/05/2015&nbsp;23:41:20");var&nbsp;date2&nbsp;=&nbsp;new&nbsp;Date("08/06/2015&nbsp;02:56:32");var&nbsp;diff&nbsp;=&nbsp;date2.getTime()&nbsp;-&nbsp;date1.getTime();var&nbsp;msec&nbsp;=&nbsp;diff;var&nbsp;hh&nbsp;=&nbsp;Math.floor(msec&nbsp;/&nbsp;1000&nbsp;/&nbsp;60&nbsp;/&nbsp;60);msec&nbsp;-=&nbsp;hh&nbsp;*&nbsp;1000&nbsp;*&nbsp;60&nbsp;*&nbsp;60;var&nbsp;mm&nbsp;=&nbsp;Math.floor(msec&nbsp;/&nbsp;1000&nbsp;/&nbsp;60);msec&nbsp;-=&nbsp;mm&nbsp;*&nbsp;1000&nbsp;*&nbsp;60;var&nbsp;ss&nbsp;=&nbsp;Math.floor(msec&nbsp;/&nbsp;1000);msec&nbsp;-=&nbsp;ss&nbsp;*&nbsp;1000;alert(hh&nbsp;+&nbsp;":"&nbsp;+&nbsp;mm&nbsp;+&nbsp;":"&nbsp;+&nbsp;ss);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript