检查Javascript中的时差

IT技术 javascript date datetime
2021-01-23 21:21:33

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

6个回答

凑合。减去 JavaScriptDate对象以获得它们的差异:

// use a constant date (e.g. 2000-01-01) and the desired time to initialize two dates

var date1 = new Date(2000, 0, 1,  9, 0); // 9:00 AM
var 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 AM

if (date2 < date1) {
    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) {
    var part = s.match(/(\d+):(\d+)(?: )?(am|pm)?/i);
    var hh = parseInt(part[1], 10);
    var mm = parseInt(part[2], 10);
    var ap = part[3] ? part[3].toUpperCase() : null;
    if (ap === "AM") {
        if (hh == 12) {
            hh = 0;
        }
    }
    if (ap === "PM") {
        if (hh != 12) {
            hh += 12;
        }
    }
    return { hh: hh, mm: mm };
}
parseTime("12:00 AM"); // {hh:  0, mm: 0}
parseTime("12:00 PM"); // {hh: 12, mm: 0}
parseTime("01:00 PM"); // {hh: 13, mm: 0}
parseTime("23:00");    // {hh: 23, mm: 0}
我如何获得日期 ="2016/03/01 11:00" 到日期 ="2016/03/06 11:00" 之间的时差
2021-03-17 21:21:33
我永远不知道这种格式的日期是否会被解析为 d/m/y 或 m/d/y。接受的官方格式在这里(无论 Date.parse() 接受什么都是有效的),developer.mozilla.org/ en/JavaScript/Reference/Global_Objects/...
2021-03-19 21:21:33
我知道这是古老的,但是在所有这些胡言乱语中可以从表单字段中读取任何内容吗?我所看到的只是一些硬编码的示例代码。我如何获得用户在我的两个input type="text"框中输入的时间之间的差异,而不是上午 9:00 和下午 5:00 之间的差异?
2021-03-31 21:21:33
这里的 +1 是什么?日期的哪一部分是递增的?
2021-04-06 21:21:33
@SalmanA:谢谢!这是非常有帮助的。为什么从当前版本中删除了它?
2021-04-09 21:21:33

此函数返回与日期时间字符串和当前日期时间不同的字符串。

function get_time_diff( datetime )
{
    var datetime = typeof datetime !== 'undefined' ? datetime : "2014-01-01 01:02:03.123456";

    var datetime = new Date( datetime ).getTime();
    var now = new Date().getTime();

    if( isNaN(datetime) )
    {
        return "";
    }

    console.log( datetime + " " + now);

    if (datetime < now) {
        var milisec_diff = now - datetime;
    }else{
        var milisec_diff = datetime - now;
    }

    var days = Math.floor(milisec_diff / 1000 / 60 / (60 * 24));

    var date_diff = new Date( milisec_diff );

    return days + " Days "+ date_diff.getHours() + " Hours " + date_diff.getMinutes() + " Minutes " + date_diff.getSeconds() + " Seconds";
}

在谷歌浏览器中测试console(按 F12)

get_time_diff()
1388534523123 1375877555722
"146 Days 12 Hours 49 Minutes 27 Seconds"
我如何获得日期 = "2016/03/01 11:00" 到日期 ="2016/03/06 11:00" 之间的时差。以及我如何为上述功能格式化日期
2021-03-26 21:21:33
momentjs.com/docs/#/displaying/difference是当你需要计算矩对象之间的差异时
2021-03-28 21:21:33
momentjs.com将解决您在 javascript 中处理时间和日期时的问题
2021-04-09 21:21:33

这是对我有用的解决方案:

var date1 = new Date("08/05/2015 23:41:20");
var date2 = new Date("08/06/2015 02:56:32");

var diff = date2.getTime() - date1.getTime();

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;

alert(hh + ":" + mm + ":" + ss);

这是我的演绎......

function get_time_difference(earlierDate, laterDate) 
{
    var oDiff = new Object();

    //  Calculate Differences
    //  -------------------------------------------------------------------  //
    var nTotalDiff = laterDate.getTime() - earlierDate.getTime();

    oDiff.days = Math.floor(nTotalDiff / 1000 / 60 / 60 / 24);
    nTotalDiff -= oDiff.days * 1000 * 60 * 60 * 24;

    oDiff.hours = Math.floor(nTotalDiff / 1000 / 60 / 60);
    nTotalDiff -= oDiff.hours * 1000 * 60 * 60;

    oDiff.minutes = Math.floor(nTotalDiff / 1000 / 60);
    nTotalDiff -= oDiff.minutes * 1000 * 60;

    oDiff.seconds = Math.floor(nTotalDiff / 1000);
    //  -------------------------------------------------------------------  //

    //  Format Duration
    //  -------------------------------------------------------------------  //
    //  Format Hours
    var hourtext = '00';
    if (oDiff.days > 0){ hourtext = String(oDiff.days);}
    if (hourtext.length == 1){hourtext = '0' + hourtext};

    //  Format Minutes
    var mintext = '00';
    if (oDiff.minutes > 0){ mintext = String(oDiff.minutes);}
    if (mintext.length == 1) { mintext = '0' + mintext };

    //  Format Seconds
    var sectext = '00';
    if (oDiff.seconds > 0) { sectext = String(oDiff.seconds); }
    if (sectext.length == 1) { sectext = '0' + sectext };

    //  Set Duration
    var sDuration = hourtext + ':' + mintext + ':' + sectext;
    oDiff.duration = sDuration;
    //  -------------------------------------------------------------------  //

    return oDiff;
}
hourtext 使用 oDiff.days,并且小时不用于计算持续时间
2021-03-17 21:21:33
注意:持续时间缺少天数
2021-03-18 21:21:33

一个好的解决方案是可用的

http://blogs.digitss.com/javascript/calculate-datetime-difference-simple-javascript-code-snippet/

以您想要的不同格式给出输出

天:小时:分钟:秒。

该代码的稍微修改版本如下所示

 var vdaysdiff; // difference of the dates
   var vhourDiff;
   var vmindiff;
   var vsecdiff;

   vdaysdiff = Math.floor(diff/1000/60/60/24);  // in days
   diff -= vdaysdiff*1000*60*60*24;

   vhourDiff = Math.floor(diff/1000/60/60);  // in hours
   diff -= vhourDiff*1000*60*60;

   vmindiff = Math.floor(diff/1000/60); // in minutes
   diff -= vmindiff*1000*60;

   vsecdiff= Math.floor(diff/1000);  // in seconds

   //Text formatting
   var hourtext = '00';
   if (hourDiff > 0){ hourtext = String(hourDiff);}
   if (hourtext.length == 1){hourtext = '0' + hourtext};                                                              

   var mintext = '00';                           
   if (mindiff > 0){ mintext = String(mindiff);}
   if (mintext.length == 1){mintext = '0' + mintext};

  //shows output as HH:MM ( i needed shorter duration)
   duration.value= hourtext + ':' + mintext;