javascript Jquery - 以 HH:MM 格式获取两个日期之间的时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8891325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:53:46  来源:igfitidea点击:

Jquery - Get the time in HH:MM format between two dates

javascriptjqueryjquery-ui-datepicker

提问by maaz

I am getting the values from two text fields as date

我从两个文本字段中获取值作为日期

var start_actual_time = $("#startPoint_complete_date").val();
var end_actual_time = $("#endPoint_complete_date").val();

which gives value

这给出了价值

start_actual_time  =  01/17/2012 11:20
end_actual_time    =  01/18/2012 12:20

now i want to find out the duration in HH:MM format between these two dates (which is 25:00 in this case) how can i do it...

现在我想找出这两个日期之间的 HH:MM 格式的持续时间(在这种情况下是 25:00)我该怎么做...

回答by Diode

var start_actual_time  =  "01/17/2012 11:20";
var end_actual_time    =  "01/18/2012 12:25";

start_actual_time = new Date(start_actual_time);
end_actual_time = new Date(end_actual_time);

var diff = end_actual_time - start_actual_time;

var diffSeconds = diff/1000;
var HH = Math.floor(diffSeconds/3600);
var MM = Math.floor(diffSeconds%3600)/60;

var formatted = ((HH < 10)?("0" + HH):HH) + ":" + ((MM < 10)?("0" + MM):MM)
alert(formatted);

See demo : http://jsfiddle.net/diode/nuv7t/5/( change mootools in jsfiddle or open http://jsfiddle.net/nuv7t/564/)

见演示:http: //jsfiddle.net/diode/nuv7t/5/(在jsfiddle改变mootools或打开http://jsfiddle.net/nuv7t/564/

回答by Sunil Raj

Working example:

工作示例:

gives alert message as 6:30

在 6:30 给出警报消息

$(function(){
    var startdate=new Date("01/17/2012 11:20");
    var enddate=new Date("01/18/2012 12:20");
    var diff = new Date(enddate - startdate);   
    alert(diff.getHours()+":"+diff.getMinutes());
});

回答by Andrew Hymanman

First, I recommend this: http://www.mattkruse.com/javascript/date/to convert the string to a date object, but you can convert it any way you want. Once converted, you can do this:

首先,我推荐这个:http: //www.mattkruse.com/javascript/date/将字符串转换为日期对象,但您可以以任何方式转换它。转换后,您可以执行以下操作:

var difference_datetime = end_datetime.getTime() - start_datetime.getTime()

The getTime()function gets the number of milliseconds since 1970/01/01. Now you have the number of milliseconds of difference, and you can do whatever you want to convert to higher numbers (eg. divide by 1000 for seconds, divide that by 60 for minutes, divide that by 60 for hours, etc.)

getTime()函数获取自 1970/01/01 以来的毫秒数。现在你有了差异的毫秒数,你可以做任何你想转换成更高数字的事情(例如,除以 1000 表示秒,除以 60 表示分钟,除以 60 表示小时,等等)

回答by Teun Pronk

I did something simular on a blog to see how long im together with my gf :P http://daystogether.blogspot.com/and this is how I did it:

我在博客上做了一些类似的事情,看看我和我的女朋友在一起多久了 :P http://daystogether.blogspot.com/我就是这样做的:

// *****Set the unit values in milliseconds.*****

var msecPerMinute = 1000 * 60;
var msecPerHour = msecPerMinute * 60;
var msecPerDay = msecPerHour * 24;

// *****Setting dates*****

var today = new Date();
var startDate = new Date('10/27/2011 11:00:00');

// *****Calculate time elapsed, in MS*****
var interval = today.getTime() - startDate.getTime();

var days = Math.floor(interval / msecPerDay );
interval = interval - (days * msecPerDay );

var weeks = 0;
while(days >= 7)
{
days = days - 7;
weeks = weeks + 1;
}

var months = 0;
while(weeks >= 4)
{
weeks = weeks - 4;
months = months + 1;
}


// Calculate the hours, minutes, and seconds.
var hours = Math.floor(interval / msecPerHour );
interval = interval - (hours * msecPerHour );

var minutes = Math.floor(interval / msecPerMinute );
interval = interval - (minutes * msecPerMinute );

var seconds = Math.floor(interval / 1000 );

BTW this is just javascript, no jquery ;)

顺便说一句,这只是 javascript,没有 jquery ;)

回答by redmoon7777

here you go:

干得好:

function get_time_difference(start,end)
{               
    start = new Date(start);
    end = new Date(end);
    var diff = end.getTime() - start.getTime();                 
    var time_difference = new Object();

    time_difference.hours = Math.floor(diff/1000/60/60);        
    diff -= time_difference.hours*1000*60*60;
    if(time_difference.hours < 10) time_difference.hours = "0" + time_difference.hours;

    time_difference.minutes = Math.floor(diff/1000/60);     
    diff -= time_difference.minutes*1000*60;    
    if(time_difference.minutes < 10) time_difference.minutes = "0" + time_difference.minutes;                                  

    return time_difference;              
}

var time_difference = get_time_difference("01/17/2012 11:20", "01/18/2012 12:20");

alert(time_difference.hours + ":" + time_difference.minutes);

回答by devzorg

start_date.setTime(Date.parse(start_actual_time));
end_date.setTime(Date.parse(end_actual_time));

//for check
start_date.toUTCString()
end_date.toUTCString()

/**
 * Different in seconds
 * @var diff int
 */

var diff = (end_date.getTime()-start_date.getTime())/1000;